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

min*_*hua 76 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
Run Code Online (Sandbox Code Playgroud)

在另一台机器上尝试相同的命令,它运作良好:

$ 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
Run Code Online (Sandbox Code Playgroud)

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

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

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

mar*_*rlo 149

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

  1. 跑(感谢@jthill):

    git remote set-branches origin '*'
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在那之后,做一个 git fetch -v

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


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

例如,更改以下行:

fetch = +refs/heads/master:refs/remotes/origin/master
Run Code Online (Sandbox Code Playgroud)

到(替换master*):

fetch = +refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)

  • 您还可以对所有分支使用`git remote set-branches origin'*'`,将`*`替换为一个branchname. (51认同)
  • 这违背了浅克隆的目的。 (10认同)
  • 对我来说,第 2 步是“git fetch -v --depth=1”,以保持我下载的数据量可以承受。 (9认同)
  • `git remote set-branches origin '*'` 有一个小问题,星号周围的命令中的 `'*'` 单引号会导致问题。该命令将 git 配置文件更改为 `fetch = +refs/heads/'*':refs/remotes/origin/'*'`,这会导致问题并且不会获取存储库中的剩余分支。 (4认同)
  • @kawing-chiu 如果您有这么多分支机构,并且以前的“互联网连接”规模太大,现在可以负担得起所有这些分支机构,这将很有用。:) (2认同)
  • 我稍微尝试了一下这种方法。我有一个相对较大的存储库(很多分支,每个分支中的内容并不多)。常规“git克隆”后,“.git”文件夹的大小为2.82GB。如果我只执行“clone --depth=1”,则为 18MB。执行此答案中描述的步骤后,大小变为 121MB。所以我不会说“这是毫无意义的”。我将使用这种方法来看看它在实践中是如何运作的。 (2认同)

Flo*_*sch 54

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

florianb$ git branch -a
        * master
          remotes/origin/HEAD -> origin/master
          remotes/origin/master
Run Code Online (Sandbox Code Playgroud)

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

florianb$ git branch -a
        * master
          remotes/origin/HEAD -> origin/master
          remotes/origin/debian
          remotes/origin/master
          remotes/origin/python_codegen
Run Code Online (Sandbox Code Playgroud)

浅克隆

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

作为-除了-的文档git clone--single-branch-option描述:

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

因此,一个浅克隆(深度 -选项)只只有一个单一的分支(以您所要求的深度).


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

  • --no-single-branch 还会克隆所有标签。我们可以通过创建一个新的 repo 来避免这种情况,使用相同的配置来获取所有遥控器,即`fetch = +refs/heads/*:refs/remotes/origin/*`,并运行`git fetch --depth 1`(没有`--tags`)。我们还可以添加要获取的特定标签,使用类似 `fetch = +refs/tags/v2.0.0:refs/tags/v2.0.0` 的配置。 (2认同)

ale*_*dnm 49

通过阅读@jthill的回复和评论,对我来说最有效的方法是使用命令中的set-branches选项git remote:

$ 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
Run Code Online (Sandbox Code Playgroud)

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

  • 使用`git remote set-branches --add origin'remote_branch_name'`可能会更好,这样新的分支就是现有的分支,而不是在远程的分支(或分支模式)列表中替换它们来获取.git/config文件. (11认同)
  • OMG,单引号`'` 在`git remote set-branches --add origin 'remote_branch_name'` 中很重要 (2认同)