我在~/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),这样新的存储库就有一个主分支,默认情况下,它可以推入和拉出local_repo的rails分支。我如何实现这一点,或者也许类似于local_repo跟踪主local_repo?


当前回答

这里有很多答案,其中提到:

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中的远程分支?

其他回答

一种方法是执行以下操作。

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

主要有两种解决方案:

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 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.

可以在2步内完成吗

克隆存储库 Git克隆<http url> 签出您想要的分支 git checkout $BranchName

为了克隆一个特定的分支,你可以做:

git clone --branch yourBranchName git@yourRepository.git

你可以使用这个命令git单个分支和重命名文件夹,如果你想保持分支独立

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

例子

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