如何在 Git 中克隆单个分支?
我在 ~/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_repo
的 local_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
只需 2 步即可完成
克隆存储库
签出您想要的分支
Can be done in 2 steps
Clone the repository
Checkout the branch you want
注意:git1.7.10(2012 年 4 月)实际上允许您仅克隆一个分支:
注意:
是远程存储库的 URL,并不引用克隆的分支;
是要克隆存储库的本地文件夹,您可以在
t5500-fetch-pack.sh
:东武 评论:
并且从 Git 1.9.0(2014 年 2 月)开始,浅克隆支持数据传输(推/拉),因此选项现在更加有用。
更多信息请参见“
git clone --depth 1
(浅克隆)比它显示的更有用吗?< /a>”。“撤消”浅层克隆的详细信息请参见“将浅层克隆转换为完整克隆”(git 1.8.3+)
正如 Chris 评论:
使用 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 日)Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:
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 repositoryYou can see it in
t5500-fetch-pack.sh
:Tobu comments that:
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+)
As Chris comments:
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)一种方法是执行以下命令。
其中
branch_name
是您选择的分支,“/your/folder”是该分支的目标文件夹。确实,这将带来其他分支,让您有机会来回合并。更新
现在,从 Git 1.7.10 开始,您现在可以执行此操作
One way is to execute the following.
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 版本 1.7.3.1(在 Windows 上),这就是我所做的(
$BRANCH
是我要签出的分支的名称,$REMOTE_REPO
是该分支的 URL我想从中克隆远程存储库):这种方法的优点是后续的 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):The advantage of this approach is that subsequent
git pull
(orgit fetch
) calls will also just download the requested branch.您可以尝试冗长的方法:
它的作用是:
希望这会是你所追求的。
You can try the long-winded way:
What this does is:
Hopefully that will be something like what you are after.
只需输入 URL 和分支名称即可。
Just put in URL and branch name.
您可以使用以下命令来完成此操作:
You can do it by using the below command:
来自 git-clone 手册页:
--single-branch
是克隆过程中的朋友记住与
--branch
一起使用,否则只会克隆远程主 HEAD(默认为 master)始终记住执行 Ctrl + F5< /kbd> 读取新的源代码,而不是来自缓存的源代码:-)
(我没有,所以很长一段时间都不知道这个选项。)
From git-clone man page:
--single-branch
is your friend during cloneremember 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.)
仅克隆一个分支。这是最简单的方法:
Clone only one branch. This is the easiest way:
这里有足够的答案提到:
下载 1 分支(带有
--single-branch
部分):...或某些版本,其中一些仅提到:
下载所有分支(不带
--single-branch
部分):但是,我想稍微解释一下这两件事并展示一个更熟悉的一组等效命令,以便我们可以了解每个底层发生的情况。
假设您在 GitHub 上有一个远程存储库,网址为 https://github.com/micronucleus/micronucleus.git,带有远程分支
master
和version_2.5
(这是一个您现在可以实际运行的真实示例)。上面第二个命令的细分:
第二个命令 (
git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git
) 克隆所有远程分支到您的本地 PC,但随后检查version_2.5
分支而不是master
分支。该命令相当于执行以下操作:-b version_2.5
部分自动为我们签出version_2.5
分支,而不是master
>。然而,
git Branch -a
向我们展示了所有分支都已克隆到我们的本地 PC。在这里,您可以看到我们所在的本地分支version_2.5
,以及本地存储的远程跟踪分支origin/HEAD
(指向origin/master
),加上origin/master
和origin/version_2.5
:我们还可以看看我们的
fetch
参考文献是。您可以打开 .git/config 文件直接查看它们,或者直接运行 git config remote.origin.fetch :您可以在上面看到我们的
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,并且还会检查它。该命令相当于执行此操作(至少在最终结果中,除了它在开始时下载的数据少得多,因为它只克隆一个分支而不是全部):-b version_2.5< /code> 部分导致默认情况下检出
version_2.5
分支而不是master
分支(如上所述),并且--single -branch
部分导致:git fetch
被配置为没有其他分支当我们调用git fetch
或git pull
时,分支将会被获取!这个命令真正克隆了,并且只会获取我们想要的一个分支,并且就是这样!
gitbranch -a
向我们展示了只有version_2.5
分支被克隆并检出。在这里,我们通过*
看到哪个分支已签出,我们还看到我们有一个origin/version_2 的本地存储远程跟踪分支。 5
:我们还可以查看
fetch
引用是什么。您可以打开 .git/config 文件直接查看它们,或者直接运行 git config remote.origin.fetch :您可以在上面看到我们的
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 个同事正在处理的分支!参考文献:
There are ample answers here which mention:
Download 1 branch (with the
--single-branch
part):...or some version of that, and a few which mention just:
Download all branches (withOUT the
--single-branch
part):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
andversion_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 theversion_2.5
branch instead of themaster
branch. That one command is the equivalent of doing this:The
-b version_2.5
part automatically checked out theversion_2.5
branch for us instead ofmaster
.git branch -a
shows us that ALL branches, however, were cloned to our local PC. Here you can see our local branchversion_2.5
, which we are on, plus the locally-stored remote-tracking branchesorigin/HEAD
(which points toorigin/master
), plusorigin/master
, andorigin/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 rungit config remote.origin.fetch
:You can see above that our
git fetch
command (which is also triggered bygit pull
since that is equivalent togit fetch && git merge
) is configured to fetch ALL heads for ALL branches in theorigin
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 theversion_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):The
-b version_2.5
part caused theversion_2.5
branch to be checked out instead of themaster
branch by default (as previously explained above), and the--single-branch
part caused:git fetch
to be configured such that NONE of the other branches will ever be fetched when we callgit fetch
orgit 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 theversion_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 fororigin/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 rungit config remote.origin.fetch
:You can see above that our
git fetch
command will only fetch theversion_2.5
branch head from theorigin/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 thebranch_name
branch is checked-out after the clone, but still clones ALL remote branches, whereas adding also--single-branch
ensures that ONLYbranch_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 themaster
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:
或者
or
我这样做了
git clone -b网址
示例:
或
I did it this way
git clone -b <branch_name> url
example :
or
????️ 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:
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:
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.)
要克隆特定分支,您可以执行以下操作:
For cloning a specific branch you can do :
要克隆您没有公钥的 Git 分支,请使用以下命令:
For cloning a branch of Git you don't have the public key to, use this:
有点晚了,但我想添加我用来解决这个问题的解决方案。我找到了解决方案
不管怎样,问题似乎是在问“如何从另一个存储库的分支启动一个新项目?”
为此,我使用的解决方案是首先在 github 或任何地方创建一个新的存储库。这将作为您的新项目的存储库。
在本地计算机上,导航到具有要用作新项目模板的分支的项目。
运行命令:
这会将 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:
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.
让我们以 Flask 仓库为例。除了master之外,它还有3个分支。让我们签出 1.1.x 远程分支,
将 git repo
cd 克隆到 repo 中。
获取远程分支 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
cd into the repo.
fetch remote branch 1.1.x
checkout the branch
You will switch to the branch 1.1.x and it will track the remote 1.1.x branch.
打开cmd。
只需一个命令:
Open the cmd.
Just one command:
主要有 2 个解决方案:
您需要使用 -b 命令开关指定分支名称。以下是克隆特定 git 分支的命令语法。
示例:
以下命令将从 git 存储库克隆分支 tahir。上述命令仅克隆特定分支,但获取其他分支的详细信息。您可以使用命令查看所有分支详细信息。
您可以使用
--single-branch
标志来防止获取其他分支的详细信息,如下所示:示例:
现在,如果您执行
gitbranch-a
,它只会显示您当前克隆的单个分支,而不是所有分支。所以这取决于你想要的方式。There are primarily 2 solutions for this:
You need to specify the branch name with -b command switch. Here is the syntax of the command to clone the specific git branch.
Example:
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.
You can use
--single-branch
flag to prevent fetching details of other branches like below:Example:
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.使用 Github CLI
如果您使用 Github CLI 克隆存储库,则必须以不同方式声明克隆选项。
--深度=1
--单分支
--分支
Usign Github CLI
If you are cloning a repo using the Github CLI, clone options must be declared differently.
--depth=1
--single-branch
--branch
如果您想要浅克隆,可以使用以下命令执行此操作:
--depth=1
意味着--single-branch
。If you want a shallow clone, you can do this with:
--depth=1
implies--single-branch
.你可以使用此命令来 git singlebranch 并重命名文件夹
如果你想保持分支独立示例,
you can use this command to git single branch and rename the folder if you want to keep branched stand alone
example
这应该有效
This should work
类似于@nosaiba-darwish在这里所说的:这里
这是我们在公司通常做的事情:
Similar to what @nosaiba-darwish said here: here
This is what we usually do in our company:
对于我这样的新手来说
只需运行下面的代码
For the newbies like me,
just run the code below
示例:
{branch-name}
{repo-URI}
Example:
{branch-name}
{repo-URI}