如何仅获取远程Git存储库的一个分支?


246

我想获取一个远程存储库的单个分支(不是全部),并创建一个本地跟踪分支,该跟踪分支可以跟踪对该远程分支的进一步更新。远程存储库中的其他分支很大,因此我想避免获取它们。我该怎么做呢?

编辑:我自己想出了这一点,但StackOverflow拒绝让我提供答案作为答案,因此我将其放在问题中。

您可以使用-t选项来git remote add,例如:

git remote add -t remote-branch remote-name remote-url

您可以使用多个“ -t branch”选项来获取多个分支。


3
git克隆将获取整个存储库,包括所有分支。您可以制作浅表副本,但这仅允许您指定修订的数量,而不指定分支的数量。
Ikke

当然您可以回答自己的问题,为什么不呢?
CharlesB 2011年

2
我认为OP意味着他那时无法回答。StackOverflow设置了您可以回答自己的问题之前必须经过的最短时间。
拉尔斯·诺丁

1
嗨,OP,现在添加您的答案可能很好。这样我们就可以对其进行投票(并与其他答案进行比较)
lulalala 2015年

3
您的git remote add命令正在添加一个远程分支,这与获取分支无关!
克里斯·哈克罗

Answers:



67

一种方法如下:

git fetch <remotename> <remote branch>:refs/remotes/<remotename>/<local branch>

但是,这不会设置跟踪。

有关更多信息,请参见git fetch的文档

编辑如下 @ user1338062 所述:如果您还没有想要在其中添加新分支的存储库的本地克隆,但是您想创建一个新的本地存储库,则git clone --branch <branch_name> --single-branch <repo_url>提供了一个较短的解决方案。


1
谢谢,这确实符合操作员的要求。进行跟踪分支git checkout -t <远程分支> / <分支名称> <分支名称>
ken

7
如果我只是这样做git checkout <branchname>,我会看到git自动建立了跟踪分支,如果具有该名称的唯一分支是远程分支。
bukzor 2013年

1
这是唯一对我有用的东西。即使配置被设置为仅提取单个分支的引用,它仍然使用fetch或pull将引用拉到所有分支。这个命令只拉了我想要的分支,所以仓库只有500kb而不是5mb。谢谢。
herostwist

1
这个答案可能已经过时了。git clone现在支持--branch--single-branch选项,我用演示发布了答案
user1338062 2015年

3
@ user1338062这仍然是有效的,如果你想只(不是所有的分支机构使用Git获取)获取一个新的分支克隆后
rubyprince


30

要更新现有遥控器以跟踪特定分支,请仅使用:

git remote set-branches <remote-name> <branch-name>

来自git help remote

set-branches
    Changes the list of branches tracked by the named remote. This can be used to track a subset of the available remote branches
    after the initial setup for a remote.

    The named branches will be interpreted as if specified with the -t option on the git remote add command line.

    With --add, instead of replacing the list of currently tracked branches, adds to that list.

1
太神奇了,我当时在--single-branch存储库中,以后无法下载其他分支。谢谢!
Yajo

22

一种方法:

在.git / config中,应将远程仓库的访存设置为访存任何分支:

   [remote "origin"]
            fetch = +refs/heads/*:refs/remotes/origin/*

获取远程分支:

git fetch origin branch-name

创建本地分支“分支名称”,该分支设置为从源头跟踪远程分支“分支名称”。

git checkout -b branch-name origin/branch-name

列出所有分支

git branch -a

16

从作者的帖子中复制:

使用-t选项git remote add,例如:

git remote add -t remote-branch remote-name remote-url

您可以使用多个-t branch选项来获取多个分支。


10
不好意思,为什么您要在同一主题中使用现有答案?
博文

13

如果要将“ git pull”和“ git fetch”的默认值更改为仅获取特定分支,则可以编辑.git / config,以便远程配置如下所示:

[remote "origin"]
  fetch = +refs/heads/master:refs/remotes/origin/master

默认情况下,这只会从原始位置获取母版。看到更多信息:https : //git-scm.com/book/en/v2/Git-Internals-The-Refspec

编辑:刚刚意识到这是-t选项为git remote add做的一件事。如果您不希望删除远程服务器并使用-t重新添加,至少在添加远程服务器后,这是一种不错的方法。


1
progit.org/book/ch9-5链接已经转移到git-scm.com/book/en/Git-Internals-The-Refspec
这里

8

为了完整起见,以下是重新签出的示例命令:

git clone --branch gh-pages --single-branch git://github.com/user/repo

如其他答案所述,它的设置remote.origin.fetch如下:

[remote "origin"]
        url = git://github.com/user/repo
        fetch = +refs/heads/gh-pages:refs/remotes/origin/gh-pages

5

git版本:2.74

这是我的方法:

git remote add [REMOTE-NAME] [REMOTE-URL]
git fetch [REMOTE-NAME] -- [BRANCH]

4

git版本2.16.1.windows.4

只需执行git fetch remoteRepositoryName branchName (eg: git fetch origin my_local_branch)就足够了。将完成提取操作,并使用相同的名称创建一个新的本地分支,并将跟踪设置为远程分支。

然后执行git checkout branchName


git branch直到您签入分支,分支才会出现在下方
commonpike

3

我的解决方法:

git fetch --depth=1
git checkout <branch_name>

如果您没有本地克隆:

git clone --depth 1 -b <branch_name> <repo_url>

3
  1. 选择<remote_name>您要使用的任何内容(随意使用origin 并跳过第1步。)
  2. git remote add <remote_name> <remote_url>
  3. git fetch <remote_name> <branch>
  4. 选择任何<your_local_branch_name>您想使用的。可能与相同<branch>
  5. git checkout <remote_name>/<branch> -b <your_local_branch_name>

希望有帮助!


1

答案实际上取决于您拥有的跟踪分支的当前列表。git fetch <remote_name> <branch_name> 仅当分支已经在跟踪分支列表中时才可以从远程获取特定分支(可以使用进行检查git branch -r)。

假设我以前使用--single-branch选项克隆了远程服务器,在这种情况下,我仅有的一个跟踪分支是“克隆”分支。我有些困惑,因为建议手动调整git config以及键入git remote add <remote_name> <remote_url>命令。当“ git remote add”设置一个新的远程服务器时,它显然不适用于现有的远程存储库;提供“ -t分支”选项对我没有帮助。

如果远程存在,并且您要获取的分支存在于该远程中:

  1. 检查git branch -r是否可以将此分支视为跟踪分支。如果不是(例如,在我的单个分支克隆中),则使用--add选项通过“ git remote set-branches”将此分支添加到跟踪分支列表:

    • git remote set-branches --add <remote_name> <branch_name>
  2. 现在,您可以获取特定的分支(此处与其他答案重复):

    • git fetch <remote_name> <branch_name>
  3. 并使用“ checkout --track”创建并签出一个新的本地分支,该分支将被赋予与跟踪分支相同的“ branch_name”:

    • git checkout --track <remote_name>/<branch_name>

0

最简单的方法

  git fetch origin <branch> && git checkout <branch>

示例:我想从原点获取uat分支并将其切换为当前工作分支。

   git fetch origin uat && git checkout uat
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.