Git在第一次检出分支时,如果没有远程名称,将无法检出分支。

3

我克隆了一个仓库并将我的分支作为另一个远程仓库添加,但是第一次无法检出一个分支(在这种情况下是master,它不是他们的默认分支)而不指定哪个远程仓库。

git clone https://github.com/facebook/zstd.git
cd zstd
git remote add jay https://github.com/jay/zstd.git
git fetch jay
git checkout master

error: pathspec 'master' did not match any file(s) known to git.

如果我指定远程主机,它可以工作:

git checkout -b master origin/master

Branch master set up to track remote branch master from origin.
Switched to a new branch 'master'

我很好奇为什么在这种情况下需要指定远程分支。我可能记错了,但我相信以前并不需要这样做。对于存在于两个版本库中的任何分支,例如git checkout zstd_help,第一次都会失败,除非我明确指定了origin远程分支。

Git版本:git version 2.7.4

1个回答

3

这里的关键是添加了第二个远程库。如果你只克隆仓库并立即尝试切换到 master 分支,它将可执行:

mureinik@computer ~/src/git
$ git clone https://github.com/facebook/zstd.git
Cloning into 'zstd'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 43954 (delta 1), reused 0 (delta 0), pack-reused 43945
Receiving objects: 100% (43954/43954), 23.29 MiB | 682.00 KiB/s, done.
Resolving deltas: 100% (32256/32256), done.

mureinik@computer ~/src/git
$ cd zstd

mureinik@computer ~/src/git/zstd (dev)
$ git checkout master
Switched to a new branch 'master'
Branch 'master' set up to track remote branch 'master' from 'origin'.

一旦您添加了jay,远程分支名称master就变得不确定了(它可能指的是origin/masterjay/master),因此,当您尝试检出时会失败。

我不确定这是什么时候发生的,但是新版本的git(我正在使用2.28)将在这种情况下给出更清晰的错误消息:

# After adding the second "jay" remote

mureinik@computer ~/src/git/zstd (master)
$ git checkout verbose
hint: If you meant to check out a remote tracking branch on, e.g. 'origin',
hint: you can do so by fully qualifying the name with the --track option:
hint:
hint:     git checkout --track origin/<name>
hint:
hint: If you'd like to always have checkouts of an ambiguous <name> prefer
hint: one remote, e.g. the 'origin' remote, consider setting
hint: checkout.defaultRemote=origin in your config.
fatal: 'verbose' matched multiple (2) remote tracking branches

1
谢谢。我进行了更多的测试,发现我使用的git版本似乎允许一些具有两个远程和模糊分支名称的存储库在第一次检出这些分支时不需要指定远程,但对于像我所问的示例之类的其他存储库则不允许。 - Jay

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接