这里有很多答案提到:
下载1 个分支(带有部分--single-branch
):
# (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
...或某个版本,还有一些只提到:
下载所有分支(没有--single-branch
部分):
# (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
但是,我想稍微解释一下这两件事,并展示一组更熟悉的等效命令,这样我们就可以看到每个底层发生了什么。
假设您在 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
分支。该命令相当于执行此操作:
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/master
号和origin/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
部分导致默认version_2.5
签出分支而不是master
分支(如前所述),并且该--single-branch
部分导致:
- 没有其他分支可以克隆到我们的 PC上,并且
git fetch
进行配置,以便当我们调用git fetch
或git pull
!
这个命令真正克隆了,只会获取我们想要的一个分支,就是这样!
git branch -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
命令只会从远程分支获取version_2.5
分支头。origin/version_2.5
而已!请注意,永远不会获取其他远程分支。
概括:
所以,现在你看到 using-b branch_name
基本上只是确保branch_name
分支在克隆之后被签出,但仍然克隆所有远程分支,而添加也--single-branch
确保只branch_name
克隆、获取、拉取和跟踪。任何其他远程分支都不会克隆到您的 PC 上。
就个人而言,我更喜欢-b branch_name
单独的选项,因为我希望所有分支都克隆到我的本地 PC。一个例外可能是一个巨大的、共享的单一仓库,它有几十个,甚至成百上千个远程分支。在这种情况下,只需使用-b branch_name --single-branch
克隆您关心的一个主分支并完成。例如,最好在一个巨大的单一存储库中为分支下载 50 GiB 的数据master
,而不是下载 200 GiB 的数据,这样你就可以拥有他们正在处理的 2000 个同行的分支!
参考:
- 只克隆一个分支
- 你如何停止在 Git 中跟踪远程分支?