英文:
Git cannot checkout remote branch despite `git ls-remote origin` showing it exists
问题
我在尝试检出一个我知道存在的远程分支时遇到了困难,因为它在git ls-remote
中显示出来:
$ git ls-remote
242d56fbd8d8af67df3157bd047252f5580e3df8 HEAD
242d56fbd8d8af67df3157bd047252f5580e3df8 refs/heads/master
517af0f6de9a3db846c4bde693a11ccb52092aee refs/heads/foobar
当我尝试检出时,我收到了一个错误:
$ git checkout refs/heads/foobar
error: pathspec 'refs/heads/foobar' did not match any file(s) known to git
我尝试了各种方法都没有成功:
git fetch --all
不改变任何东西,git fetch origin
也不行git branch -v -a
不显示它
通过查阅许多SO答案,我最终偶然发现了一个有效的方法,但我不知道为什么会有效:
git fetch origin foobar:foobar
英文:
I'm struggling to checkout a remote branch that I know it exists, because it show up with git ls-remote
:
$ git ls-remote
242d56fbd8d8af67df3157bd047252f5580e3df8 HEAD
242d56fbd8d8af67df3157bd047252f5580e3df8 refs/heads/master
517af0f6de9a3db846c4bde693a11ccb52092aee refs/heads/foobar
When I try to checkout, I get an error:
$ git checkout refs/heads/foobar
error: pathspec 'refs/heads/foobar' did not match any file(s) known to git
I've tried all sorts of things to no avail:
git fetch --all
doesn't change anything, neither doesgit fetch origin
git branch -v -a
doesn't show it
Going through many SO answers, I finally stumbled upon something that worked, but I have no clue why:
git fetch origin foobar:foobar
答案1
得分: 1
原来我最初使用--depth 1
来浅克隆存储库。这导致.git/config
中发生了微妙的更改。我的remote.origin.fetch
被设置为:
$ git config remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master
这意味着在运行诸如checkout
、fetch
、branch
等命令时,git永远不会查看除了主分支以外的任何内容,唯一的例外是git ls-remote
,它似乎忽略了这个配置设置。
修复很简单:
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
但要注意,这样做会取消浅克隆的好处。这样你将拉取大量的历史记录。
替代方法可能是(未经测试):git fetch --unshallow
要仅获取分支一次,请使用git fetch origin foobar:foobar
。但这种方式也会获取大量的历史记录,将来可能需要再次执行此操作。
如果你只想跟踪这个分支以及其他分支,可能可以更改fetch配置,但我不太确定具体怎么做。
由于大多数人不使用浅克隆,因此大多数现有的StackOverflow问题/答案不涉及这个特定问题。感谢kenorb在这个答案中指引我这个方向。
英文:
It turns out I had originally shallow-cloned the repository with --depth 1
. This caused a subtle change in .git/config
. My remote.origin.fetch
was set to:
$ git config remote.origin.fetch
+refs/heads/master:refs/remotes/origin/master
This meant that git never looked at the origin branch for anything but master when running things like checkout
, fetch
, branch
, with the exception of git ls-remote
which seems to ignore that config setting.
Fixing is easy:
git config remote.origin.fetch '+refs/heads/*:refs/remotes/origin/*'
though beware that this undoes the benefits of shallow cloning. You will pull quite a lot of the history that way.
Alternatives may be (untested): git fetch --unshallow
To fetch the branch just once, use git fetch origin foobar:foobar
. But this way you will also get a lot of the history and you may need to do this fetch again in the future.
If you just want to track that one branch in addition to the others, it may be possible to change the fetch config, but I'm not quite sure how exactly.
As most people don't use shallow-cloning, most existing StackOverflow questions/answers don't address this particular issue. Thanks to kenorb for pointing me this way in this answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论