英文:
Git checkout - prefer branch over tag
问题
Doing git checkout foo
会优先检出标签,并使仓库处于分离头状态。要告诉Git我想要检出分支而不是标签,您可以使用以下命令:
git checkout refs/heads/foo
不过,您提到这会出现错误:"pathspec did not match any files(s) known to git"。这可能是因为您的本地仓库没有与该分支关联。确保您的本地仓库已经同步了分支信息。
另外,您可以尝试以下命令:
git switch refs/remotes/origin/foo
但是,这也可能导致错误:"fatal: a branch is expected, got remote branch"。这是因为这个分支在远程(GitHub)上,而不在本地。您需要先克隆或拉取该分支到本地,然后再进行检出。
英文:
Suppose there is a tag and a branch with identical names.
Doing git checkout foo
prefers to check out the tag, and leaves the repo in a detached head state.
How do I tel git that I want to check out the branch not the tag?
An advice I found here https://groups.google.com/g/git-users/c/FfzGmqj6sNQ is to use git checkout refs/heads/foo
but for me this gives an error: "pathspec did not match any files(s) known to git".
Here are my refs:
some_hash1 refs/remotes/origin/foo
some_hash2 refs/tags/foo
As you can see there is no refs/heads/foo
.
Tried "git switch refs/remotes/origin/foo" and this errors out too: "fatal: a branch is expected, got remote branch".
I'm running git 2.40.0
The branch "foo" is there in GitHub web UI.
答案1
得分: 1
Found answer here:
https://www.git-tower.com/learn/git/faq/track-remote-upstream-branch
git checkout --track origin/foo
After this git show-ref
shows the local branch foo
under refs/heads
:
$ git show-ref | grep foo
some_hash1 refs/heads/foo
some_hash1 refs/remotes/origin/foo
some_hash2 refs/tags/foo
英文:
Found answer here:
https://www.git-tower.com/learn/git/faq/track-remote-upstream-branch
git checkout --track origin/foo
After this git show-ref
shows the local branch foo
under refs/heads
:
$ git show-ref | grep foo
some_hash1 refs/heads/foo
some_hash1 refs/remotes/origin/foo
some_hash2 refs/tags/foo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论