我在~/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?


当前回答

如果你想要一个浅克隆,你可以这样做:

git clone -b mybranch --depth=1 https://example.com/myproject.git localname

——depth=1表示——single-branch。

其他回答

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

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

注意:git1.7.10(2012年4月)实际上只允许你克隆一个分支:

# clone only the remote primary HEAD (default: origin/master)
git clone <url> --single-branch

# as in:
git clone <url> --branch <branch> --single-branch <folder>

注意:

<url>是远程存储库的url,不引用克隆的分支 <folder>是要克隆存储库的本地文件夹

你可以在t5500-fetch-pack.sh中看到:

test_expect_success 'single branch clone' '
  git clone --single-branch "file://$(pwd)/." singlebranch
'

Tobu评论说:

这在做浅克隆时是隐式的。 这使得git clone—depth 1成为节省带宽的最简单方法。

从Git 1.9.0(2014年2月)开始,浅克隆支持数据传输(推/拉),所以这个选项现在更有用了。 详见“git克隆——深度1(浅克隆)比它所做的更有用吗?”。


“撤销”浅克隆的详细信息见“将浅克隆转换为完整克隆”(git 1.8.3+)

# unshallow the current branch
git fetch --unshallow

# for getting back all the branches (see Peter Cordes' comment)
git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
git fetch --unshallow

正如克里斯评论的那样:

将缺失的分支反向转换为单个分支的魔法线是(git v2.1.4):

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --unshallow  

在Git 2.26 (Q1 2020)中,“Git克隆-递归-子模块-单分支”现在在克隆子模块时使用相同的单分支选项。

参见Emily Shaffer (nasamuffin)的commit 132f600, commit 4731957(2020年2月21日)。 (由Junio C Hamano合并- gitster - in commit b22db26, 05 Mar 2020)

克隆:在——递归子模块期间传递——单分支 署名:Emily Shaffer 获奖嘉宾:杰夫·金 以前,执行“git克隆—递归—子模块—单分支”会导致子模块克隆所有分支,即使超级项目只克隆了一个分支。 管道——通过子模块帮助框架的单个分支,使其稍后“克隆”。

类似于@nosaiba-darwish在这里说的:这里

我们公司通常是这样做的:

git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally

如果你想要一个浅克隆,你可以这样做:

git clone -b mybranch --depth=1 https://example.com/myproject.git localname

——depth=1表示——single-branch。

让我们以flask repo为例。除master外,它还有3个分支。让我们签出1.1。X远程分支

克隆git回购

git clone https://github.com/pallets/flask

CD进入回购。

cd flask

获取远程分支1.1.x

git fetch origin 1.1.x

签出分支

git checkout 1.1.x

您将切换到分支1.1。X,它将跟踪远程1.1。x分支。