【问题标题】:git shallow clone (clone --depth) misses remote branchesgit shallow clone (clone --depth) 错过了远程分支
【发布时间】:2014-07-05 16:06:26
【问题描述】:

克隆远程存储库后,它不会通过 -a 选项显示任何远程分支。可能是什么问题呢?如何调试它?在这个 sn-p 中没有显示两个远程分支:

$ 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

还尝试克隆另一个 repo,效果很好。虽然我可以在这台机器上再试一次,但最好知道哪里出了问题。

我们非常欢迎任何建议或提示。

编辑:答案摘要:由于 git 版本 1.8.3.2 需要一起使用“--depth”和“--no-single-branch”才能获得与以前相同的行为。这被认为是一个错误修复。

【问题讨论】:

  • master 是您当地的分支机构。 remotes/origin/master 是对应的远程分支。究竟是什么问题?
  • 您可能忘记了冗长吗?试试git branch -avv
  • 致 michas 等:我们通常不将 master 称为分支,抱歉造成混淆。添加了“未显示两个远程分支”。致jthill:谢谢提醒,你是对的。
  • 感谢您介绍git clone --depth=1 --no-single-branch,这是我大多数情况下需要的。

标签: git branch shallow-clone


【解决方案1】:

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

  1. 运行(感谢@jthill):

    git remote set-branches origin '*'
    
  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

到(将master替换为*):

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

【讨论】:

  • 您也可以将git remote set-branches origin '*' 用于所有分支,将* 替换为一个分支名称。
  • 谢谢!这节省了我的时间。
  • @guo 用于gitverbositydebug 日志记录级别。它不是来自fetch 方法。
  • 这违背了浅克隆的目的。
  • @kawing-chiu 如果您有这么多分支机构,并且以前的“互联网连接”规模太大,现在可以负担得起所有这些分支机构,这将很有用。 :)
【解决方案2】:

通过阅读@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

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

【讨论】:

  • 使用git remote set-branches --add origin 'remote_branch_name' 可能会更好,以便新分支是现有分支的补充,而不是在远程分支列表(或分支模式)中替换它们以在 .git 中获取/配置文件。
  • 天啊,' 的单引号在 git remote set-branches --add origin 'remote_branch_name' 中很重要
  • @Weekend 在我留下单引号之前我无法让它工作
  • @PandaWood 你可能在 Windows 上。答案中的“$”符号表示 Bash(在 Unix 或 Cygwin/MSYS 上)。
  • 我在the docs 中没有看到任何关于单引号的必要性,而且至少在 macOS 上没有单引号似乎可以正常工作。
【解决方案3】:

行为是正确的,在最后一次修订之后,主分支是(因为这是主远程的 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。”因此,浅克隆应该包含请求的提交深度,从分支的尖端开始。

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

“仅克隆指向单个分支尖端的历史记录,由--branch 选项指定或主分支远程的HEAD 指向。使用--depth 选项创建浅克隆时,这是默认的,除非给定--no-single-branch来获取所有分支的tips附近的历史。"

因此,浅克隆带有 depth-选项)仅获取单个分支(在您要求的深度)。


不幸的是,这两个选项(--depth--single-branch)过去都存在错误,并且使用浅克隆隐含了未解决的问题(正如您可以在我上面发布的链接中看到的那样),这是由给定的历史记录引起的-改写。这总体上会导致在特殊情况下出现一些复杂的行为。

【讨论】:

  • florianb:你的 git 版本是多少?感谢您试用。我刚才在 1.7.1 上做了 --depth 1 它显示了所有远程分支。用这个更新了这个问题。 +1 用于验证问题。
  • @minghua:我正在使用 1.8.4 - 如果有针对该问题的补丁,我会进行一些调查。
  • @minghua:我编辑以反映关于“浅层克隆”的新发现。
  • 这几乎是完美的,除了一件事:说“回购所有者决定切断其他分支”是什么意思?我认为那些分支仍然存在。
  • --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 之类的配置添加要获取的特定标签。
猜你喜欢
  • 2018-03-30
  • 2022-10-07
  • 2015-06-14
  • 2016-07-11
  • 2021-07-10
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
相关资源
最近更新 更多