这里有很多答案,其中提到:
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:
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
但是,我想稍微阐述一下这两件事,并展示一组更熟悉的等效命令,这样我们就可以看到每个命令背后发生了什么。
让我们假设您在GitHub https://github.com/micronucleus/micronucleus.git上有一个远程回购,具有远程分支master和version_2.5(这是一个您现在可以实际运行的真实示例)。
上面的第二个命令的分解:
第二个命令(git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git)将所有REMOTE分支克隆到您的本地PC上,但随后签出version_2.5分支而不是主分支。一个命令相当于这样做:
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)被配置为获取源远程中所有分支的所有头部。我不是这方面的专家,但我相信这就是+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分支而不是主分支(如上所述),而——single-branch部分导致:
没有其他分支克隆到我们的PC,和
git fetch被配置为当我们调用git fetch或git pull时,其他任何分支都不会被获取!
这个命令真正克隆了,只会获取我们想要的一个分支,就是它!
git分支-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获取命令将只从origin/version_2.5远程分支获取version_2.5分支头。就是这样!注意,永远不会获取其他远程分支。
简介:
因此,现在您可以看到,使用-b branch_name基本上只是确保在克隆之后签出branch_name分支,但仍然克隆所有远程分支,而添加also——single-branch则确保克隆、提取、提取和跟踪ONLY branch_name。任何其他远程分支都不会克隆到您的PC上。
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!
引用:
只克隆一个分支
如何停止跟踪Git中的远程分支?