简体   繁体   English

git 浅克隆 (clone --depth) 错过远程分支

[英]git shallow clone (clone --depth) misses remote branches

After cloning a remote repository it does not show any remote branch by -a option.克隆远程存储库后,它不会通过 -a 选项显示任何远程分支。 What could be the problem?可能是什么问题呢? How to debug it?如何调试它? In this snippet two of the remote branches are not shown:在这个片段中,没有显示两个远程分支:

$ 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

Tried the same command on another machine, it works well:在另一台机器上试过同样的命令,效果很好:

$ 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

Tried also cloning another repo, it works well.还尝试克隆另一个 repo,效果很好。 Though I can try it on this machine again, but it would be better to know what's wrong.虽然我可以在这台机器上再试一次,但最好知道出了什么问题。

Any suggestions or hints will be more than welcome.任何建议或提示将非常受欢迎。

Edit: Answer summary: Since git version 1.8.3.2 the "--depth" and "--no-single-branch" need to be used together to get the same behavior as before.编辑:答案摘要:从 git 版本 1.8.3.2 开始,“--depth”和“--no-single-branch”需要一起使用才能获得与以前相同的行为。 This is deemed a bug fix.这被视为错误修复。

After doing a shallow clone, to be able to checkout other branches from remote ,做一个浅克隆后,为了能够从远程检出其他分支

  1. Run (thanks @jthill):运行(感谢@jthill):

     git remote set-branches origin '*'
  2. After that, do a git fetch -v之后,执行git fetch -v

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


Step 1 can also be done manually by editing .git/config .步骤 1 也可以通过编辑.git/config手动完成。

For instance, change the folloing line from:例如,将以下行更改为:

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

to (replace master with * ):到(用*替换master ):

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

From reading the responses and the comment from @jthill, the thing that worked best for me was to use the set-branches option on the git remote command:通过阅读@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

This changes the list of branches tracked by the named remote so that we can fetch and checkout just the required branch.这会更改命名远程跟踪的分支列表,以便我们可以仅获取和检出所需的分支。

The behavior is correct, after the last revision the master-branch is (since this is the primary remote's HEAD) the only remote-branch in the repository:行为是正确的,在最后一次修订之后,主分支是(因为这是主远程的 HEAD)存储库中唯一的远程分支:

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

The full clone offers new (all) branches:完整克隆提供新的(所有)分支:

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

Shallow clones浅克隆

Due to the shallow-description in the technical documentation, a " git-clone --depth 20 repo [...] result[s in] commit chains with a length of at most 20."由于技术文档中的描述很浅,“ git-clone --depth 20 repo [...] 导致提交链长度最多为 20。” A shallow clone therefore should contain the requested depth of commits, from the tip of a branch.因此,浅克隆应该包含请求的提交深度,从分支的尖端开始。

As - in addition - the documentation of git clone for the --single-branch -option describes:此外,-- --single-branch -option 的git clone文档描述了:

"Clone only the history leading to the tip of a single branch, either specified by the --branch option or the primary branch remote's HEAD points at. When creating a shallow clone with the --depth option, this is the default, unless --no-single-branch is given to fetch the histories near the tips of all branches. " “仅克隆导致单个分支尖端的历史记录,由--branch选项指定或主分支远程的HEAD指向。使用--depth选项创建浅克隆时,这是默认值,除非--no-single-branch用于获取所有分支尖端附近的历史记录。

Therefore a shallow clone ( with the depth -option) only fetches only one single branch (at your requested depth).因此,浅层克隆带有depth 选项)仅获取一个分支(在您请求的深度)。


Unfortunately both options ( --depth and --single-branch ) have been faulty in the past and the use of shallow clones implicits unresolved problems (as you can read in the link I posted above), which is caused by the given history-rewrite.不幸的是,这两个选项( --depth--single-branch )过去都存在错误,并且浅克隆的使用隐含了未解决的问题(正如您可以在我上面发布的链接中阅读的那样),这是由给定的历史引起的-改写。 This leads in overall to somewhat complicated behavior in special cases.在特殊情况下,这会导致总体上有些复杂的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM