为什么使用 --single-branch 时 Git 无法解析远程分支?

Dus*_*rea 4 git

因此,我们经常通过使用 --single-branch 有效克隆来优化克隆。但是,我们以后无法获得额外的分支。在管道方面,带有和不带有 --single-branch 的 git clone 有什么区别?稍后我们如何获取其他分支?

标准克隆:

$ git clone -b branch-name https://repo.url standard
$ cd standard
$ git checkout remote-branch
Branch 'remote-branch' set up to track remote branch 'remote-branch' from 'origin'.
Switched to a new branch 'remote-branch'
Run Code Online (Sandbox Code Playgroud)

单分支克隆:

$ git clone -b branch-name --single-branch https://repo.url singlebranch
$ cd singlebranch
$ git checkout remote-branch
error: pathspec 'remote-branch' did not match any file(s) known to git
Run Code Online (Sandbox Code Playgroud)

更新

根据下面@AndrewMarshall 的回答,您需要更新配置中的默认 fetch refspec。即使您可以绕过 fetch 来获取正确的提交,但如果您不先修复配置,您尝试的结帐将绝对拒绝了解有关该分支的任何信息:

$ git fetch origin +refs/heads/remote-branch:refs/remotes/origin/remote-branch
From https://gerrit.magicleap.com/a/platform/mlmanifest
 * [new branch]      remote-branch -> origin/remote-branch

$ git checkout remote-branch 
error: pathspec 'remote-branch' did not match any file(s) known to git

$ git remote set-branches origin --add remote-branch
$ git checkout remote-branch 
Branch 'remote-branch' set up to track remote branch 'remote-branch' from 'origin'.
Switched to a new branch 'remote-branch'
Run Code Online (Sandbox Code Playgroud)

请注意,我们获取它,然后重新配置,然后结帐。获取可以以任何顺序发生(尽管如果不在配置中,您必须传递参数)但结帐由配置门控

And*_*all 9

--single-branch通过将远程的fetch属性设置为单个分支名称而不是 glob 来工作:

$ git config --get-all remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master
Run Code Online (Sandbox Code Playgroud)

所以让我们添加一个条目git remote set-branches

$ git remote set-branches origin --add other-branch
$ git config --get-all remote.origin.fetch    
+refs/heads/master:refs/remotes/origin/master
+refs/heads/other-branch:refs/remotes/origin/other-branch

$ git fetch
From origin
 * [new branch]      other-branch        -> origin/other-branch

$ git checkout other-branch
Branch 'other-branch' set up to track remote branch 'other-branch' from 'origin'.
Switched to a new branch 'other-branch'
Run Code Online (Sandbox Code Playgroud)

或者,或者,将其设为 glob,以便可以获取所有分支(默认的非单分支行为):

git remote set-branches origin '*'
Run Code Online (Sandbox Code Playgroud)