Git checkout -首选分支而不是标签



假设有一个标签和一个分支具有相同的名称。

git checkout foo更倾向于检查标签,并使repo处于分离的头部状态。

我如何告诉git我想检查分支而不是标签?

我在https://groups.google.com/g/git-users/c/FfzGmqj6sNQ找到的一个建议是使用git checkout refs/heads/foo,但对我来说这给出了一个错误:"pathspec不匹配任何已知的文件"。

以下是我的建议:

some_hash1 refs/remotes/origin/foo
some_hash2 refs/tags/foo

可以看到没有refs/heads/foo

已尝试"git switch refs/remotes/origin/foo"并且这个错误也会出现:&;fatal: a branch is expected, got remote branch&;

我正在运行git 2.40.0

分支"foo"在GitHub的web UI。

找到答案了:

https://www.git-tower.com/learn/git/faq/track-remote-upstream-branch

git checkout --track origin/foo

之后,git show-ref显示refs/heads下的本地分支foo:

$ git show-ref | grep foo
some_hash1 refs/heads/foo
some_hash1 refs/remotes/origin/foo
some_hash2 refs/tags/foo