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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23708231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 18:05:19  来源:igfitidea点击:

git shallow clone (clone --depth) misses remote branches

gitbranchshallow-clone

提问by minghua

After cloning a remote repository it does not show any remote branch by -a option. What could be the problem? How to debug it? In this snippet two of the remote branches are not shown:

克隆远程存储库后,它不会通过 -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

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. Though I can try it on this machine again, but it would be better to know what's wrong.

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

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. This is deemed a bug fix.

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

采纳答案by Florian Breisch

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-descriptionin the technical documentation, a "git-clone --depth 20 repo[...] result[s in] commit chains with a length of at most 20." A shallow clone therefore should contain the requested depth of commits, from the tip of a branch.

由于技术文档中的描述很肤浅,“ git-clone --depth 20 repo[...] 导致 [s] 提交链长​​度最多为 20”。因此,浅克隆应该包含请求的提交深度,从分支的尖端开始。

As - in addition - the documentation of git clonefor the --single-branch-option describes:

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

"Clone only the history leading to the tip of a single branch, either specified by the --branchoption or the primary branch remote's HEADpoints at. When creating a shallow clone with the --depthoption, this is the default, unless --no-single-branchis given to fetch the histories near the tips of all branches."

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

Therefore a shallow clone(withthe depth-option) only fetchesonly one single branch(at your requested depth).

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



Unfortunately both options (--depthand --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. This leads in overall to somewhat complicated behavior in special cases.

不幸的是,这两个选项 (--depth--single-branch) 过去都存在错误,浅层克隆的使用隐含了未解决的问题(正如您可以在我上面发布的链接中阅读的内容),这是由给定的历史重写引起的。在特殊情况下,这会导致总体上有些复杂的行为。

回答by marlo

After doing a shallow clone, to be able to checkout other branches from remote,

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

  1. Run (thanks @jthill):

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

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

  1. 运行(感谢@jthill):

    git remote set-branches origin '*'
    
  2. 之后,做一个 git fetch -v

  3. 最后 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 masterwith *):

到(替换master*):

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

回答by alejandrodnm

From reading the responses and the comment from @jthill, the thing that worked best for me was to use the set-branchesoption on the git remotecommand:

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

This changes the list of branches tracked by the named remote so that we can fetch and checkout just the required branch.

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