git 我无法签出特定分支,“分离的 HEAD 状态”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35383648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I can't checkout a specific branch, "detached HEAD state"
提问by mnordber
Me and my friend has a repo which he created. He then created a branch called "lexer" for us to work on.
我和我的朋友有一个他创建的回购。然后他创建了一个名为“lexer”的分支供我们处理。
The problem is that while he can switch forth and back between master and lexer it does not work at all for me.
问题是,虽然他可以在 master 和 lexer 之间来回切换,但对我来说根本不起作用。
Eventually I just started over (rm -rf repo
and then cloned the repo) but it's still impossible to checkout the lexer branch?
最终我只是重新开始(rm -rf repo
然后克隆了 repo)但是仍然无法签出词法分析器分支?
On a freshly cloned repo:
在新克隆的 repo 上:
git branch
gives:
git branch
给出:
$ git branch
* master
git checkout lexer
gives:
git checkout lexer
给出:
$ git checkout lexer
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
I CAN checkout origin/lexer but I end up in a detached HEAD state?
我可以结帐原点/词法分析器,但最终处于分离的 HEAD 状态?
$ git checkout origin/lexer master
Note: checking out 'origin/lexer'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
It is possible for me to push to the lexer branch by doing
我可以通过这样做来推送到词法分析器分支
git push origin HEAD:lexer
but well, I really would like to sort this mess out. It's so weird that it works for him but not for me? He says that he hasn't got any local changes from the git repo either...
但好吧,我真的很想解决这个烂摊子。太奇怪了,它对他有效,但对我无效?他说他也没有从 git repo 中得到任何本地更改......
Anyone have any clue?
有人有任何线索吗?
回答by Per Johansson
I'm going to venture a guess that you have a directory named lexer
at the top level. Since git checkout
is used both to switch branches and to reset files in the tree, it's probably detecting that you don't have a branch called lexer
but you do have a path and selects the second mode.
It works for your friend because he already has a lexer
branch.
我将冒险猜测您有一个以lexer
顶级命名的目录。由于git checkout
用于切换分支和重置树中的文件,它可能检测到您没有调用分支lexer
但您有路径并选择第二种模式。它适用于你的朋友,因为他已经有一个lexer
分支。
Easiest workaround is probably to create the branch using git branch
instead.
最简单的解决方法可能是使用创建分支git branch
。
git branch --track lexer origin/lexer
should do that for you. You can then use git checkout
to switch to it.
应该为你做那件事。然后您可以使用git checkout
切换到它。
Another option might be to use the --
flag to git checkout. I haven't tried it but I think it should work:
另一种选择可能是使用该--
标志来进行 git checkout。我还没有尝试过,但我认为它应该有效:
git checkout lexer --
When you add --
, the word before it is always considered a branch/commit/tree and the word after a path.
添加时--
,它之前的单词始终被视为分支/提交/树,而路径之后的单词始终被视为。
回答by Philippe
You can't checkout lexer
because you don't have the branch in your local repository (and surely a folder with the same name) . You only have the remote branch 'origin/lexer'.
First you have to create the local branch :
您无法结帐,lexer
因为您的本地存储库中没有分支(当然还有同名文件夹)。您只有远程分支“origin/lexer”。首先,您必须创建本地分支:
git checkout -b lexer origin/lexer
A good explaination on the subject: https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Tracking-Branches
关于这个主题的一个很好的解释:https: //git-scm.com/book/en/v2/Git-Branching-Remote-Branches#Tracking-Branches
回答by Denis Pshenov
Your probably want this:
你可能想要这个:
git checkout -t origin/lexer
From git manual:
从 git 手册:
As a convenience, --track without -b implies branch creation...
为方便起见,不带 -b 的 --track 意味着分支创建...
and
和
-t, --track... When creating a new branch, set up "upstream" configuration... If no -b option is given, the name of the new branch will be derived from the remote-tracking branch
-t, --track... 创建新分支时,设置“上游”配置...如果没有给出 -b 选项,新分支的名称将从远程跟踪分支派生
回答by David Deutsch
You get into a detached HEAD state because origin/lexer
is a remote tracking branch, and thus read only. You want to just checkout lexer
, and do your work on that. Then, when you push, origin/lexer
will be brought up to date.
您进入分离的 HEAD 状态,因为它origin/lexer
是一个远程跟踪分支,因此是只读的。你只想 checkout lexer
,然后做你的工作。然后,当您推送时,origin/lexer
将被更新。
Edit: Rereading your question, I see that you have tried to checkout lexer, but end up staying on master. It is strange that git checkout
is failing silently. Try git checkout -t origin/lexer
.
编辑:重读你的问题,我看到你试图结帐词法分析器,但最终留在主人。奇怪的git checkout
是,默默地失败了。试试git checkout -t origin/lexer
。
回答by Steliyan Georgiev
Another wild guess: you are having a tag with identical name so
另一个疯狂的猜测:你有一个同名的标签,所以
git checkout lexer
actually checks out the tag, not the branch
实际上检查标签,而不是分支
回答by Phil H
TLDR: This could just be a syntaxissue. Instead of
TLDR:这可能只是一个语法问题。代替
git checkout origin/mybranch
Try:
尝试:
git checkout mybranch
The steps git takes, given the following command:
git 采取的步骤,给出以下命令:
git checkout origin/mybranch
- git looks for a branch called 'origin/mybranch' in the local repo.
- Not finding it, it will then look for a branch called 'origin/mybranch' in the available remotes.
- Not finding that either, it tries treating that as a commit tag, and finds the tag 'origin/mybranch' against a specific commit. It checks out that commit for you, in a detached state.
- git 在本地存储库中查找名为“origin/mybranch”的分支。
- 找不到它,它会在可用的遥控器中寻找一个名为“origin/mybranch”的分支。
- 也没有找到,它尝试将其视为提交标记,并针对特定提交找到标记“origin/mybranch”。它以分离状态为您检查该提交。
But that wasn't what you wanted! You wanted the branch called 'mybranch'. If there's only one remote with such a branch (and nothing locally to confuse matters) then git will assume you meant this usage, filling in <remote>
for you:
但这不是你想要的!您想要名为“mybranch”的分支。如果只有一个遥控器有这样的分支(并且本地没有任何事情可以混淆),那么 git 会假设您是指这种用法,并<remote>
为您填写:
git checkout -b <branch> --track <remote>/<branch>
If it can't find a branch with the matching name, it defaults to this:
如果找不到具有匹配名称的分支,则默认为:
git checkout [--detach] <commit>
Which is where the detached HEAD message comes in.
这就是分离的 HEAD 消息的来源。
See the git checkout docs, and pay careful note of <branch>
vs <remote>
.
查看git checkout 文档,并仔细注意<branch>
vs <remote>
。
回答by CodeWizard
When you do: git checkout origin/lexer master
you simply change your HEAD
to point to the latest commit in our remote
branch. It's a read only branch.
当你这样做时:git checkout origin/lexer master
你只需将你HEAD
的指向我们remote
分支中的最新提交。这是一个只读分支。
If you wish to understand what HEAD is read this :
How to move HEAD back to a previous location? (Detached head)
如果您想了解 HEAD 是什么,请阅读以下内容:
如何将 HEAD 移回以前的位置?(分离的头)
In your case you simply need to do the following:
在您的情况下,您只需执行以下操作:
Update your repository with the latest code with all branches and tags
使用包含所有分支和标签的最新代码更新您的存储库
# Update your local repository with all the remote branches
# --prune = remove all deleted data locally which was removed on the remote
git fetch --all --prune
Now you should have the latest branches locally.
现在您应该在本地拥有最新的分支。
Delete the old local branch
删除旧的本地分支
!!! IMPORTANT:
If you made modification and did not push them don't delete the local branch
!!!重要提示:
如果您进行了修改并且没有推送它们,请不要删除本地分支
git branch -D lexer
Checkout the desired branch
签出所需的分支
# don't use the remote simply the branch name
git checkout lexer
Now you have local branch with the name lexer checked out from your remote branch
现在您已经从远程分支签出名称为 lexer 的本地分支