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

时间:2019-03-26 01:47:18

标签: git

因此,我们经常通过--single-branch有效克隆来优化克隆。但是,我们随后将无法再获得其他分支。有和没有-单分支的git克隆之间有什么区别?以后如何获取其他分支?

标准克隆:

$ 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'

单分支克隆:

$ 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

更新

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

$ 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'

请注意,我们先获取它,然后重新配置,然后然后检出。提取可以以任何顺序进行(尽管如果不在配置中,则必须传递参数),但结帐由配置 gated

1 个答案:

答案 0 :(得分:3)

--single-branch的工作方式是将遥控器的fetch属性设置为单个分支名称,而不是全局名称:

$ git config --get-all remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master

所以我们用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'

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

git remote set-branches origin '*'