Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么可以';t Git在使用单分支时解析远程分支?_Git - Fatal编程技术网

为什么可以';t Git在使用单分支时解析远程分支?

为什么可以';t Git在使用单分支时解析远程分支?,git,Git,因此,我们经常通过使用单分支进行有效克隆来优化克隆。但是,我们以后无法获得其他分支。在管道方面,有和没有单分支的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-b

因此,我们经常通过使用单分支进行有效克隆来优化克隆。但是,我们以后无法获得其他分支。在管道方面,有和没有单分支的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的回答,您需要在配置中更新默认的fetch 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'
注意,我们获取它,然后重新配置,然后签出。取数可以按任何顺序进行(尽管如果不在配置中,则必须传递参数),但签出由配置进行选通。

--single branch
通过将远程设备的
取数
属性设置为仅为单个分支名称,而不是glob:

$ git config --get-all remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master
因此,让我们添加一个条目:

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


如果我能给你一个如此迅速、简洁的答案,我会给你一千分。你说得对。我没想过。那么,提供给git fetch的refspec是否只是我们正在为该远程设备审核的内容上的一个过滤器?它似乎有两个方面:
git fetch
在询问远程设备时默认使用配置的refspec,并且,当明确给出refspec时(例如
git fetch other branch
),
git fetch
只会创建/更新一个本地引用,以匹配配置好的远程引用(否则它不知道要创建/更新哪个本地引用)。实际上,签出似乎也受到它的约束。您可以直接传递建议的值,然后fetch将其降低。然而,checkout仍将坚决拒绝合作。请参阅我上面的附录中的示例。如果您提供了ref本身(例如,
git checkout other branch origin/other branch
),而不是让
git checkout
自己尝试解决它(在这种情况下,它将使用fetch config()),那么它可能会起作用。我建议一般避免
--单分支
(通常也要避免
——浅薄的
)。对于非常特定的情况,这是可以的,但如果您确实计划使用存储库,只需继续进行完整克隆。如果存储库很大,一开始会很痛苦;之后,后续的获取通常相当轻。这对于开发人员系统来说是有意义的,但对于短暂的构建工作区来说就没有意义了。是的,尽管例如,当Jenkins制作浅层克隆(也是单分支克隆)时,一些明智的想法,例如将
master
与某个开发分支进行比较,会停止工作。使用
--depth=50
会导致更神秘的失败:几乎所有的构建都可以工作,但也有一些无法工作,并且需要很长时间才能意识到,是长的提交链正在断裂!
$ 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 '*'