这里有很多答案提到:
-
下载 1 分支(with --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 命令只会从origin/version_2.5 远程分支获取version_2.5 分支头。而已!请注意,不会获取任何其他远程分支。
总结:
所以,现在您看到使用-b branch_name 基本上只是确保在克隆后签出branch_name 分支,但仍会克隆所有远程分支,同时添加--single-branch 确保仅克隆branch_name 、提取、拉取和跟踪。 任何其他远程分支都不会克隆到您的 PC。
就个人而言,我更喜欢单独使用-b branch_name 选项,因为我希望将所有 分支克隆到我的本地PC。一个例外可能是一个巨大的、共享的单一仓库,它有几十个,甚至成百上千个远程分支。在这种情况下,只需使用-b branch_name --single-branch 克隆您关心的一个主分支并完成。例如,最好在一个巨大的单一存储库中为 master 分支下载 50 GiB 的数据,而不是下载 200 GiB 的数据,这样您就可以拥有他们正在处理的 2000 个同行的分支!
参考资料:
- Clone only one branch
- How do you stop tracking a remote branch in Git?