git shallow clone(clone --depth)错过了远程分支

时间:2014-05-17 06:13:59

标签: git branch shallow-clone

克隆远程存储库后,它不会通过-a选项显示任何远程分支。可能是什么问题呢?怎么调试呢?在此片段中,未显示两个远程分支:

$ git clone --depth 1 git://git.savannah.gnu.org/pythonwebkit.git
$ cd pythonwebkit
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
$ git --version
git version 1.8.3.1

在另一台机器上尝试了相同的命令,效果很好:

$ git clone --depth 1 git://git.savannah.gnu.org/pythonwebkit.git
Receiving objects: 100% (186886/186886), 818.91 MiB | 3.44 MiB/s, done.
$ cd pythonwebkit/
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/debian
  remotes/origin/master
  remotes/origin/python_codegen
$ git --version
git version 1.7.1

尝试克隆另一个回购,它运作良好。虽然我可以在这台机器上再试一次,但最好知道什么是错的。

任何建议或提示都将受到欢迎。

编辑:答案摘要:从git版本1.8.3.2开始,需要同时使用“--depth”和“--no-single-branch”来获得与以前相同的行为。这被视为错误修复。

3 个答案:

答案 0 :(得分:114)

做了浅克隆后, 能够从远程结帐其他分支

  1. 运行(感谢@jthill):

    git remote set-branches origin '*'
    
  2. 之后,请执行git fetch -v

  3. 最后git checkout the-branch-i-ve-been-looking-for


  4. 步骤1也可以通过编辑.git/config手动完成。

    例如,更改以下行:

    fetch = +refs/heads/master:refs/remotes/origin/master
    

    to(将master替换为*):

    fetch = +refs/heads/*:refs/remotes/origin/*
    

答案 1 :(得分:52)

行为是正确的,在最后一次修订之后,master-branch(因为这是主要远程的HEAD)是存储库中唯一的远程分支:

florianb$ git branch -a
        * master
          remotes/origin/HEAD -> origin/master
          remotes/origin/master

完整克隆提供新的(所有)分支:

florianb$ git branch -a
        * master
          remotes/origin/HEAD -> origin/master
          remotes/origin/debian
          remotes/origin/master
          remotes/origin/python_codegen

浅克隆

由于技术文档中的shallow-description,“git-clone --depth 20 repo [...]结果[s in]提交链的长度最多为20”。因此,浅层克隆应该包含从分支的尖端提交的请求深度。

另外 - --single-branch选项的git clone文档描述:

  

“仅克隆导致单个分支的提示的历史记录,由--branch选项或主分支远程的HEAD指定。创建浅层克隆时使用--depth选项,这是默认选项,除非--no-single-branch用于获取所有分支提示附近的历史记录。

因此,浅克隆 深度 - 选项)仅提取一个分支(在您要求的深度)。


不幸的是,两个选项(--depth--single-branch)在过去都是错误的,并且浅克隆的使用意味着未解决的问题(正如您在上面发布的链接中所读到的那样),这是由此引起的由给定的历史重写。在特殊情况下,这导致总体上有些复杂的行为。

答案 2 :(得分:44)

通过阅读@jthill的回复和评论,最适合我的是在git remote命令中使用set-branches选项:

$ git clone --depth 1 https://github.com/dogescript/dogescript.git
$ git remote set-branches origin 'remote_branch_name'
$ git fetch --depth 1 origin remote_branch_name
$ git checkout remote_branch_name

这会更改指定远程跟踪的分支列表,以便我们可以只获取和签出所需的分支。

相关问题