如何在 Git 中克隆单个分支?

发布于 2024-08-12 05:47:18 字数 585 浏览 7 评论 0原文

我在 ~/local_repo 中有一个本地 Git 存储库。它有几个分支:

$ git branch
* master
  rails
  c
  c++

要克隆本地存储库,我这样做:

$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/

new_repo master 分支指向 local_repo master 分支,我可以推送/拉取。

但我无法克隆另一个分支。我只想拉出我想要的分支(例如 rails),以便新存储库有一个 master 分支,可以从 local_repo 推送和拉取> 的 rails 分支,默认情况下。我该如何完成此任务,或者与跟踪主 local_repolocal_repo 类似的事情?

I have a local Git repository in ~/local_repo. It has a few branches:

$ git branch
* master
  rails
  c
  c++

To clone the local repository, I do:

$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/

The new_repo master branch points to the local_repo master branch, and I can push / pull.

But I am unable to clone another branch. I want to only pull the branch I want (e.g. rails), so that the new repository has a master branch that pushes to and pulls from local_repo's rails branch, by default. How do I accomplish this, or perhaps something similar with local_repo tracking the master local_repo?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(27

花开雨落又逢春i 2024-08-19 05:47:19

只需 2 步即可完成

  1. 克隆存储库

    git clone ;
    
  2. 签出您想要的分支

    git checkout $BranchName
    

Can be done in 2 steps

  1. Clone the repository

    git clone <http url>
    
  2. Checkout the branch you want

    git checkout $BranchName
    
冷清清 2024-08-19 05:47:18

注意:git1.7.10(2012 年 4 月)实际上允许您仅克隆一个分支

# clone only the remote primary HEAD (default: origin/master)
git clone <url> --single-branch

# as in:
git clone <url> --branch <branch> --single-branch <folder>

注意:

  • 是远程存储库的 URL,并不引用克隆的分支
  • ; 是要克隆存储库的本地文件夹,

您可以在 t5500-fetch-pack.sh

test_expect_success '单分支克隆' '
  git clone --single-branch "file://$(pwd)/."单枝
'

东武 评论

这在进行浅克隆时是隐式的。
这使得 git clone --depth 1 成为节省带宽的最简单方法。

并且从 Git 1.9.0(2014 年 2 月)开始,浅克隆支持数据传输(推/拉),因此选项现在更加有用。
更多信息请参见“git clone --depth 1(浅克隆)比它显示的更有用吗?< /a>”。


“撤消”浅层克隆的详细信息请参见“将浅层克隆转换为完整克隆”(git 1.8.3+)

# 取消当前分支的浅度
git fetch --unshallow

# 取回所有分支(参见 Peter Cordes 的评论)
git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
git fetch --unshallow

正如 Chris 评论:

用于反转丢失分支的魔法线--single-branch是(git v2.1.4):

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --unshallow  

使用 Git 2.26 (Q1 2020),“git clone --recurse-submodules -- single-branch现在在克隆子模块时使用相同的单分支选项

请参阅 提交 132f600提交 4731957(2020 年 2 月 21 日),作者:Emily Shaffer (nasamuffin)
(由 Junio C Hamano -- gitster -- 合并于 提交 b22db26,2020 年 3 月 5 日)

克隆:在期间传递--single-branch --recurse-子模块

签字人:Emily Shaffer
确认者:Jeff King

以前,执行“git clone --recurse-submodules --single-branch”会导致子模块克隆所有分支,即使超级项目仅克隆一个分支。

通过子模块帮助程序框架传送--single-branch,以便稍后将其“克隆”。

Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:

# clone only the remote primary HEAD (default: origin/master)
git clone <url> --single-branch

# as in:
git clone <url> --branch <branch> --single-branch <folder>

Note:

  • <url> is the URL of the remote repository, and does not reference the branch cloned
  • <folder> is the local folder where you want to clone the repository

You can see it in t5500-fetch-pack.sh:

test_expect_success 'single branch clone' '
  git clone --single-branch "file://$(pwd)/." singlebranch
'

Tobu comments that:

This is implicit when doing a shallow clone.
This makes git clone --depth 1 the easiest way to save bandwidth.

And since Git 1.9.0 (February 2014), shallow clones support data transfer (push/pull), so that option is even more useful now.
See more at "Is git clone --depth 1 (shallow clone) more useful than it makes out?".


"Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (git 1.8.3+)

# unshallow the current branch
git fetch --unshallow

# for getting back all the branches (see Peter Cordes' comment)
git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
git fetch --unshallow

As Chris comments:

the magic line for getting missing branches to reverse --single-branch is (git v2.1.4):

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --unshallow  

With Git 2.26 (Q1 2020), "git clone --recurse-submodules --single-branch" now uses the same single-branch option when cloning the submodules.

See commit 132f600, commit 4731957 (21 Feb 2020) by Emily Shaffer (nasamuffin).
(Merged by Junio C Hamano -- gitster -- in commit b22db26, 05 Mar 2020)

clone: pass --single-branch during --recurse-submodules

Signed-off-by: Emily Shaffer
Acked-by: Jeff King

Previously, performing "git clone --recurse-submodules --single-branch" resulted in submodules cloning all branches even though the superproject cloned only one branch.

Pipe --single-branch through the submodule helper framework to make it to 'clone' later on.

仙气飘飘 2024-08-19 05:47:18

一种方法是执行以下命令。

git clone user@git-server:project_name.git -b branch_name /your/folder

其中 branch_name 是您选择的分支,“/your/folder”是该分支的目标文件夹。确实,这将带来其他分支,让您有机会来回合并。

更新

现在,从 Git 1.7.10 开始,您现在可以执行此操作

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder

One way is to execute the following.

git clone user@git-server:project_name.git -b branch_name /your/folder

Where branch_name is the branch of your choice and "/your/folder" is the destination folder for that branch. It's true that this will bring other branches giving you the opportunity to merge back and forth.

Update

Now, starting with Git 1.7.10, you can now do this

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder
燕归巢 2024-08-19 05:47:18

使用 Git 版本 1.7.3.1(在 Windows 上),这就是我所做的($BRANCH 是我要签出的分支的名称,$REMOTE_REPO 是该分支的 URL我想从中克隆远程存储库):

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH

这种方法的优点是后续的 git pull (或 git fetch )调用也将只下载请求的分支。

Using Git version 1.7.3.1 (on Windows), here's what I do ($BRANCH is the name of the branch I want to checkout and $REMOTE_REPO is the URL of the remote repository I want to clone from):

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH

The advantage of this approach is that subsequent git pull (or git fetch) calls will also just download the requested branch.

三生路 2024-08-19 05:47:18

您可以尝试冗长的方法:

mkdir newrepo.git
cd newrepo.git
git init
git remote add origin file:///path/to/original
git fetch origin branchiwant:refs/remotes/origin/branchiwant
git checkout -b branchiwant --track origin/branchiwant

它的作用是:

  • 创建并初始化一个空的 Git 存储库。
  • 将原始存储库添加为名为 origin 的远程存储库。
  • 仅从名为origin的远程获取您需要的分支。
  • 创建并签出一个新分支,该分支设置为跟踪您刚刚克隆的源分支。

希望这会是你所追求的。

You can try the long-winded way:

mkdir newrepo.git
cd newrepo.git
git init
git remote add origin file:///path/to/original
git fetch origin branchiwant:refs/remotes/origin/branchiwant
git checkout -b branchiwant --track origin/branchiwant

What this does is:

  • Create and init an empty Git repository.
  • Adds the original repository as a remote called origin.
  • Fetches only the branch you require from the remote called origin.
  • Creates and checks out a new branch that is set up to track the source branch you just cloned.

Hopefully that will be something like what you are after.

拒绝两难 2024-08-19 05:47:18
git clone <url> --branch <branch> --single-branch

只需输入 URL 和分支名称即可。

git clone <url> --branch <branch> --single-branch

Just put in URL and branch name.

无语# 2024-08-19 05:47:18

您可以使用以下命令来完成此操作:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in

You can do it by using the below command:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in
花期渐远 2024-08-19 05:47:18

来自 git-clone 手册页

--single-branch 是克隆过程中的朋友
记住与 --branch 一起使用,否则只会克隆远程主 HEAD(默认为 master)

始终记住执行 Ctrl + F5< /kbd> 读取新的源代码,而不是来自缓存的源代码:-)
(我没有,所以很长一段时间都不知道这个选项。)

From git-clone man page:

--single-branch is your friend during clone
remember to use with --branch <branch name> or only remote primary HEAD will be cloned (master by default)

Always remember to do Ctrl + F5 to read fresh source, not the one from cache :-)
(I didn't so didn't know about this option for long time.)

不疑不惑不回忆 2024-08-19 05:47:18

仅克隆一个分支。这是最简单的方法:

git clone -b BRANCH_NAME --single-branch [email protected]:___/PROJECTNAME.git

Clone only one branch. This is the easiest way:

git clone -b BRANCH_NAME --single-branch [email protected]:___/PROJECTNAME.git
浮生未歇 2024-08-19 05:47:18

这里有足够的答案提到:

  1. 下载 1 分支(带有 --single-branch 部分):

    # (下面我们将其称为“第一个命令”。)
    # 1. 仅克隆 `branch_name`,然后检查它。 `--single-branch` 部分
    # 表示仅克隆、获取、拉取“branch_name”
    # 跟踪。 _不会将其他远程分支克隆到您的 PC
    # 无论如何._
    git clone -b 分支名称 --single-branch \
    https://github.com/some_project/some_project.git
    

    ...或某些版本,其中一些仅提到:

  2. 下载所有分支(不带--single-branch部分):

    # (下面我们将其称为“第二个命令”。)
    # 2. 克隆所有远程分支,然后 `git checkout` 为 `branch_name`
    # 分支。
    git clone -b 分支名称 \
    https://github.com/some_project/some_project.git
    

但是,我想稍微解释一下这两件事并展示一个更熟悉的一组等效命令,以便我们可以了解每个底层发生的情况。

假设您在 GitHub 上有一个远程存储库,网址为 https://github.com/micronucleus/micronucleus.git,带有远程分支 masterversion_2.5(这是一个您现在可以实际运行的真实示例)。

上面第二个命令的细分:

第二个命令 (git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git) 克隆所有远程分支到您的本地 PC,但随后检查 version_2.5 分支而不是 master 分支。该命令相当于执行以下操作:

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5
# To be pedantic, also delete the local `master` branch since
# technically it won't exist yet since you haven't yet checked
# it out with `git checkout master`, which would create it from
# your locally-stored remote-tracking branch, `origin/master`
git branch -d master

-b version_2.5 部分自动为我们签出 version_2.5 分支,而不是 master >。

然而,git Branch -a 向我们展示了所有分支都已克隆到我们的本地 PC。在这里,您可以看到我们所在的本地分支 version_2.5,以及本地存储的远程跟踪分支 origin/HEAD (指向 origin/master),加上 origin/masterorigin/version_2.5

$ git branch -a
* version_2.5
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/version_2.5

我们还可以看看我们的 fetch 参考文献是。您可以打开 .git/config 文件直接查看它们,或者直接运行 git config remote.origin.fetch :

$ git config remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*

您可以在上面看到我们的 git fetch 命令(也由 git pull 触发,因为它相当于 git fetch && git merge)被配置为获取所有头origin 远程中的所有分支。我不是这方面的专家,但我相信这就是 +refs/heads/*:refs/remotes/origin/* 的意思。

上面第一个命令的细分:

第一个命令 (git clone -b version_2.5 --single-branch https://github.com/micronucleus/micronucleus.git) 仅将 version_2.5 分支克隆到您的本地 PC,并且还会检查它。该命令相当于执行此操作(至少在最终结果中,除了它在开始时下载的数据少得多,因为它只克隆一个分支而不是全部):

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5

# Delete ALL other branches, including remote-tracking ones, which are not the 
# `version_2.5` branch:
# One local branch
git branch -d master
# ALL other locally-stored remote-tracking branches
git branch -dr origin/HEAD 
git branch -dr origin/master

# Fix your `.git/config` file so that `git fetch` does the right thing, fetching
# ONLY the `version_2.5` branch head from the `origin/version_2.5` remote branch:
git config remote.origin.fetch \
"+refs/heads/version_2.5:refs/remotes/origin/version_2.5"

-b version_2.5< /code> 部分导致默认情况下检出 version_2.5 分支而不是 master 分支(如上所述),并且 --single -branch 部分导致:

  1. 没有任何其他分支被克隆到我们的 PC,并且
  2. git fetch 被配置为没有其他分支当我们调用 git fetchgit pull 时,分支将会被获取!

这个命令真正克隆了,并且只会获取我们想要的一个分支,并且就是这样!

gitbranch -a 向我们展示了只有 version_2.5 分支被克隆并检出。在这里,我们通过 * 看到哪个分支已签出,我们还看到我们有一个 origin/version_2 的本地存储远程跟踪分支。 5

$ git branch -a
* version_2.5
  remotes/origin/version_2.5

我们还可以查看 fetch 引用是什么。您可以打开 .git/config 文件直接查看它们,或者直接运行 git config remote.origin.fetch :

$ git config remote.origin.fetch
+refs/heads/version_2.5:refs/remotes/origin/version_2.5

您可以在上面看到我们的 git fetch 命令只会从 origin/version_2.5 远程分支获取 version_2.5 分支头。就是这样!请注意,不会获取任何其他远程分支。

摘要:

所以,现在您看到使用 -bbranch_name 基本上只是确保在克隆后签出 branch_name 分支,但仍然克隆所有远程分支,同时添加--single-branch 确保仅克隆、获取、拉取和跟踪 branch_name不会将任何其他远程分支克隆到您的 PC。

就我个人而言,我更喜欢单独使用 -bbranch_name 选项,因为我希望克隆所有分支到我的本地电脑。一个例外可能是在一个巨大的、共享的单一存储库上,它有数十个、甚至数百个或数千个远程分支。在这种情况下,只需使用 -bbranch_name --single-branch 来克隆您关心的一个主分支即可完成。例如,在一个巨大的 mono-repo 中为 master 分支下载 50 GiB 的数据比下载 200 GiB 的数据更好,这样你就可以拥有 2000 个同事正在处理的分支!

参考文献:

  1. 仅克隆一个分支
  2. 如何停止跟踪 Git 中的远程分支?

There are ample answers here which mention:

  1. Download 1 branch (with the --single-branch part):

    # (We'll refer to this as "the 1st command" below.)
    # 1. Clone ONLY `branch_name`, then check it out. The `--single-branch` part
    #    means that ONLY `branch_name` is cloned, fetched, pulled, and
    #    tracked. _No other remote branches will be cloned to your PC
    #    whatsoever._
    git clone -b branch_name --single-branch \
    https://github.com/some_project/some_project.git
    

    ...or some version of that, and a few which mention just:

  2. Download all branches (withOUT the --single-branch part):

    # (We'll refer to this as "the 2nd command" below.)
    # 2. Clone ALL remote branches, then `git checkout` the `branch_name`
    #    branch.
    git clone -b branch_name \
    https://github.com/some_project/some_project.git
    

But, I'd like to expound upon these two things a bit and show a more familiar set of equivalent commands so we can see what is happening with each under-the-hood.

Let's assume that you have a remote repo on GitHub at https://github.com/micronucleus/micronucleus.git, with remote branches master and version_2.5 (this is a real example you can actually run right now).

Breakdown of the 2nd command from above:

The 2nd command (git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git) clones ALL REMOTE BRANCHES to your local PC, but then checks out the version_2.5 branch instead of the master branch. That one command is the equivalent of doing this:

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5
# To be pedantic, also delete the local `master` branch since
# technically it won't exist yet since you haven't yet checked
# it out with `git checkout master`, which would create it from
# your locally-stored remote-tracking branch, `origin/master`
git branch -d master

The -b version_2.5 part automatically checked out the version_2.5 branch for us instead of master.

git branch -a shows us that ALL branches, however, were cloned to our local PC. Here you can see our local branch version_2.5, which we are on, plus the locally-stored remote-tracking branches origin/HEAD (which points to origin/master), plus origin/master, and origin/version_2.5:

$ git branch -a
* version_2.5
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/version_2.5

We can also look at what our fetch references are. You can either open up the .git/config file to see them directly, or just run git config remote.origin.fetch:

$ git config remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*

You can see above that our git fetch command (which is also triggered by git pull since that is equivalent to git fetch && git merge) is configured to fetch ALL heads for ALL branches in the origin remote. I'm not an expert on this part, but I believe that's what +refs/heads/*:refs/remotes/origin/* means.

Breakdown of the 1st command from above:

The 1st command (git clone -b version_2.5 --single-branch https://github.com/micronucleus/micronucleus.git) clones ONLY the version_2.5 branch to your local PC, and it also checks it out. That one command is the equivalent of doing this (in the end result at least, except that it also downloads much less data in the beginning since it only clones ONE branch NOT all of them):

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5

# Delete ALL other branches, including remote-tracking ones, which are not the 
# `version_2.5` branch:
# One local branch
git branch -d master
# ALL other locally-stored remote-tracking branches
git branch -dr origin/HEAD 
git branch -dr origin/master

# Fix your `.git/config` file so that `git fetch` does the right thing, fetching
# ONLY the `version_2.5` branch head from the `origin/version_2.5` remote branch:
git config remote.origin.fetch \
"+refs/heads/version_2.5:refs/remotes/origin/version_2.5"

The -b version_2.5 part caused the version_2.5 branch to be checked out instead of the master branch by default (as previously explained above), and the --single-branch part caused:

  1. NONE of the other branches to be cloned to our PC, and
  2. git fetch to be configured such that NONE of the other branches will ever be fetched when we call git fetch or git pull!

This command truly cloned and will fetch only the one branch we wanted, and that's it!

git branch -a shows us that ONLY the version_2.5 branch was cloned and checked out. Here we see by the * which branch is checked-out, and we see also that we have a locally-stored remote-tracking branch for origin/version_2.5:

$ git branch -a
* version_2.5
  remotes/origin/version_2.5

We can also look at what our fetch references are. You can either open up the .git/config file to see them directly, or just run git config remote.origin.fetch:

$ git config remote.origin.fetch
+refs/heads/version_2.5:refs/remotes/origin/version_2.5

You can see above that our git fetch command will only fetch the version_2.5 branch head from the origin/version_2.5 remote branch. That's it! Beware that no other remote branches will ever be fetched.

Summary:

So, now you see that using -b branch_name basically just ensures the branch_name branch is checked-out after the clone, but still clones ALL remote branches, whereas adding also --single-branch ensures that ONLY branch_name is cloned, fetched, pulled, and tracked. No other remote branches will be cloned to your PC whatsoever.

Personally, I prefer the -b branch_name option alone, because I want all branches cloned to my local PC. The one exception might be on a huge, shared mono-repo which has dozens, or even hundreds or thousands of remote branches. In that case, just use -b branch_name --single-branch to clone just the one main branch you care about and be done. Better to download 50 GiB of data for the master branch in a huge mono-repo, for instance, than to download 200 GiB of data so you can have 2000 of your peers' branches they are working on too!

References:

  1. Clone only one branch
  2. How do you stop tracking a remote branch in Git?
拥醉 2024-08-19 05:47:18
git clone --branch <branchname> <remote-repo-url>

或者

git clone -b <branchname> <remote-repo-url>
git clone --branch <branchname> <remote-repo-url>

or

git clone -b <branchname> <remote-repo-url>
北斗星光 2024-08-19 05:47:18

我这样做了

git clone -b网址

示例:

git clone -b master https://gitlab.com/jhondoe/applicationproject.git

git clone -b master [email protected]:jhondoe/applicationproject.git

I did it this way

git clone -b <branch_name> url

example :

git clone -b master https://gitlab.com/jhondoe/applicationproject.git

or

git clone -b master [email protected]:jhondoe/applicationproject.git
梦亿 2024-08-19 05:47:18

????️ Some Performance Measurements ????️

I compared two of the suggested approaches, and found that one is way faster (and smaller) than the other (which is advantageous if your only goal is to clone, and you don't care about much else).

I found that the most important performance indicator is --depth, meaning that VonC's answer is the fastest.
Try it yourself:

time bash -cl "git clone --single-branch --depth=1 --branch=$MYBRANCH $MYGITURL"

I tested this with a big project with a long history and many branches, and this approach takes about 6s.

Note that, contrary to what is claimed by several comments in this thread, (at least in a recent git version), this will only checkout the target branch. git branch -a only lists that single branch.

The Runner Up

By comparison, frerich-rabe's approach consistently took 26s:

time bash -cl "git init && git remote add -t $MYBRANCH -f origin $MYGITURL && git checkout $MYBRANCH"

When comparing the two, it is also important to note that, in my case, the target branch is a much slimmed down version of its parent branch. The first approach's download progress bar reflects that reduction in size (< 10MB), while the second does not (> 90MB). (I'm sure, there are more mature methods to measure total download size, but I have not looked into that yet.)

黎歌 2024-08-19 05:47:18

要克隆特定分支,您可以执行以下操作:

git clone --branch yourBranchName [email protected]

For cloning a specific branch you can do :

git clone --branch yourBranchName [email protected]
皓月长歌 2024-08-19 05:47:18

要克隆您没有公钥的 Git 分支,请使用以下命令:

git clone -b <branch> <Git repository URL or clone URL you get from Git repository>

For cloning a branch of Git you don't have the public key to, use this:

git clone -b <branch> <Git repository URL or clone URL you get from Git repository>
乖乖 2024-08-19 05:47:18
git clone --single-branch -b [branch name]  [repository URL]
git clone --single-branch -b [branch name]  [repository URL]
离笑几人歌 2024-08-19 05:47:18

有点晚了,但我想添加我用来解决这个问题的解决方案。我找到了解决方案

不管怎样,问题似乎是在问“如何从另一个存储库的分支启动一个新项目?”

为此,我使用的解决方案是首先在 github 或任何地方创建一个新的存储库。这将作为您的新项目的存储库。

在本地计算机上,导航到具有要用作新项目模板的分支的项目。

运行命令:

git push https://github.com/accountname/new-repo.git +old_branch:master

这会将 old_branch 推送到 new-repo 并使其成为新存储库的主分支。

然后,您只需将新存储库克隆到新项目的本地目录,并且您就可以在旧分支上启动一个新项目。

A little late but I wanted to add the solution I used to solve this problem. I found the solution here.

Anyway, the question seems to be asking 'how to start a new project from a branch of another repo?'

To this, the solution I used would be to first create a new repo in github or where ever. This will serve as the repo to your new project.

On your local machine, navigate to the project that has the branch you want to use as the template for your new project.

Run the command:

git push https://github.com/accountname/new-repo.git +old_branch:master

What this will do is push the old_branch to new-repo and make it the master branch of the new repo.

You then just have to clone the new repo down to your new project's local directory and you have a new project started at the old branch.

想你的星星会说话 2024-08-19 05:47:18

让我们以 Flask 仓库为例。除了master之外,它还有3个分支。让我们签出 1.1.x 远程分支,

将 git repo

git clone https://github.com/pallets/flask

cd 克隆到 repo 中。

cd flask

获取远程分支 1.1.x

git fetch origin 1.1.x

签出分支

git checkout 1.1.x

您将切换到分支 1.1.x,它将跟踪远程 1.1.x 分支。

Let us take the example of flask repo. It has 3 branches in addition to master. Let us checkout the 1.1.x remote branch

clone the git repo

git clone https://github.com/pallets/flask

cd into the repo.

cd flask

fetch remote branch 1.1.x

git fetch origin 1.1.x

checkout the branch

git checkout 1.1.x

You will switch to the branch 1.1.x and it will track the remote 1.1.x branch.

一百个冬季 2024-08-19 05:47:18

打开cmd。

cd folder_name  # enter the path where to clone the branch

只需一个命令:

git clone url_of_projecturltoclone -b branch_name

Open the cmd.

cd folder_name  # enter the path where to clone the branch

Just one command:

git clone url_of_projecturltoclone -b branch_name
吃素的狼 2024-08-19 05:47:18

主要有 2 个解决方案:

  1. 您需要使用 -b 命令开关指定分支名称。以下是克隆特定 git 分支的命令语法。

    git clone -b ; ;
    

    示例:

    git clone -b tahir https://github.com/Repository/Project.git
    

    以下命令将从 git 存储库克隆分支 tahir。上述命令仅克隆特定分支,但获取其他分支的详细信息。您可以使用命令查看所有分支详细信息。

    git 分支 -a
    
  2. 您可以使用 --single-branch 标志来防止获取其他分支的详细信息,如下所示:

    git clone -b ; --single-branch ;
    

    示例:

    git clone -b tahir --single-branch \ 
    https://github.com/Repository/Project.git
    

    现在,如果您执行gitbranch-a,它只会显示您当前克隆的单个分支,而不是所有分支。所以这取决于你想要的方式。

There are primarily 2 solutions for this:

  1. You need to specify the branch name with -b command switch. Here is the syntax of the command to clone the specific git branch.

    git clone -b <BRANCH_NAME> <GIT_REMOTE_URL>
    

    Example:

    git clone -b tahir https://github.com/Repository/Project.git
    

    The following command will clone the branch tahir from the git repository.The above command clones only the specific branch but fetches the details of other branches. You can view all branches details with command.

    git branch -a
    
  2. You can use --single-branch flag to prevent fetching details of other branches like below:

    git clone -b <BRANCH_NAME> --single-branch <GIT_REMOTE_URL>
    

    Example:

    git clone -b tahir --single-branch \ 
    https://github.com/Repository/Project.git
    

    Now if you do a git branch -a, it will only show your current single branch that you have cloned and not all the branches. So it depends on you how you want it.

风筝有风,海豚有海 2024-08-19 05:47:18

使用 Github CLI

如果您使用 Github CLI 克隆存储库,则必须以不同方式声明克隆选项。

--深度=1

gh repo clone username/repo -- --depth=1

--单分支

gh repo clone username/repo -- --single-branch

--分支

gh repo clone username/repo -- --branch main --single-branch localDir

Usign Github CLI

If you are cloning a repo using the Github CLI, clone options must be declared differently.

--depth=1

gh repo clone username/repo -- --depth=1

--single-branch

gh repo clone username/repo -- --single-branch

--branch

gh repo clone username/repo -- --branch main --single-branch localDir
甜味超标? 2024-08-19 05:47:18

如果您想要浅克隆,可以使用以下命令执行此操作:

git clone -b mybranch --depth=1 https://example.com/myproject.git localname

--depth=1 意味着--single-branch

If you want a shallow clone, you can do this with:

git clone -b mybranch --depth=1 https://example.com/myproject.git localname

--depth=1 implies --single-branch.

握住你手 2024-08-19 05:47:18

你可以使用此命令来 git singlebranch 并重命名文件夹

git clone -b [branch-name] --single-branch [url]  [folder_name]

如果你想保持分支独立示例,

git clone -b mybranch --single-branch git://github/repository.git  project_mybranch

you can use this command to git single branch and rename the folder if you want to keep branched stand alone

git clone -b [branch-name] --single-branch [url]  [folder_name]

example

git clone -b mybranch --single-branch git://github/repository.git  project_mybranch
芸娘子的小脾气 2024-08-19 05:47:18

这应该有效

git clone --single-branch <branchname> <remote-repo-url>
git clone --branch <branchname> <remote-repo-url>

This should work

git clone --single-branch <branchname> <remote-repo-url>
git clone --branch <branchname> <remote-repo-url>
左岸枫 2024-08-19 05:47:18

类似于@nosaiba-darwish在这里所说的:这里

这是我们在公司通常做的事情:

git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally

Similar to what @nosaiba-darwish said here: here

This is what we usually do in our company:

git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally
绿光 2024-08-19 05:47:18

对于我这样的新手来说
只需运行下面的代码

git clone https://gitlab.com/repo/repo.git --branch <name of branch> --single-branch

For the newbies like me,
just run the code below

git clone https://gitlab.com/repo/repo.git --branch <name of branch> --single-branch
甜宝宝 2024-08-19 05:47:18
git clone --branch {branch-name} {repo-URI}

示例:

git clone --branch dev https://github.com/ann/cleaningmachine.git
git clone --branch {branch-name} {repo-URI}

Example:

git clone --branch dev https://github.com/ann/cleaningmachine.git
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文