为什么我的 Git 存储库进入分离的 HEAD 状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3965676/
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
Why did my Git repo enter a detached HEAD state?
提问by Adam Bergmark
I ended up with a detached head today, the same problem as described in: git push says everything up-to-date even though I have local changes
我今天结束了一个独立的头,与描述的相同的问题:git push 说所有内容都是最新的,即使我有本地更改
As far as I know I didn't do anything out of the ordinary, just commits and pushes from my local repo.
据我所知,我没有做任何不寻常的事情,只是从我的本地仓库提交和推送。
So how did I end up with a detached HEAD
?
那么我是如何结束的detached HEAD
呢?
采纳答案by VonC
Any checkout of a commit that is not the name of one of yourbranches will get you a detached HEAD. A SHA1 which represents the tip of a branch still gives a detached HEAD. Only a checkout of a local branch nameavoids that mode.
任何不是您的一个分支名称的提交的检出都会为您提供一个分离的 HEAD。代表分支尖端的 SHA1 仍然给出一个分离的 HEAD。只有签出本地分支名称才能避免这种模式。
See committing with a detached HEAD
When HEAD is detached, commits work like normal, except no named branch gets updated. (You can think of this as an anonymous branch.)
当 HEAD 被分离时,提交工作正常,除了没有命名分支被更新。(您可以将其视为匿名分支。)
For example, if you checkout a "remote branch" without tracking it first, you can end up with a detached HEAD.
例如,如果您签出一个“远程分支”而没有先跟踪它,您最终可能会得到一个分离的 HEAD。
See git: switch branch without detaching head
With Git 2.23 (August 2019), you don't have to use the confusing git checkout
commandanymore.
使用 Git 2.23(2019 年 8 月),您不必再使用令人困惑的git checkout
命令。
git switch
can also checkout a branch, and get a detach HEAD, except:
git switch
也可以签出一个分支,并获得一个分离的 HEAD,除了:
- it has an explicit
--detach
option
- 它有一个明确的
--detach
选项
To check out commit
HEAD~3
for temporary inspection or experiment without creating a new branch:git switch --detach HEAD~3 HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
要在
HEAD~3
不创建新分支的情况下检查提交以进行临时检查或实验:git switch --detach HEAD~3 HEAD is now at 9fc9555312 Merge branch 'cc/shared-index-permbits'
- it cannot detached by mistake a remote tracking branch
- 它不能错误地分离远程跟踪分支
See:
看:
C:\Users\vonc\arepo>git checkout origin/master
Note: switching to 'origin/master'.
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 switching back to a branch.
Vs. using the new git switch
command:
对比 使用新git switch
命令:
C:\Users\vonc\arepo>git switch origin/master
fatal: a branch is expected, got remote branch 'origin/master'
If you wanted to create a new local branch tracking a remote branch:
如果您想创建一个新的本地分支来跟踪远程分支:
git switch <branch>
If
<branch>
is not found but there does exist a tracking branch in exactly one remote (call it<remote>
) with a matching name, treat as equivalent togit switch -c <branch> --track <remote>/<branch>
如果
<branch>
未找到,但在一个<remote>
具有匹配名称的远程(称为)中确实存在跟踪分支,则视为等同于git switch -c <branch> --track <remote>/<branch>
No more mistake!
No more unwanted detached HEAD!
不要再犯错了!
没有更多不需要的分离头!
回答by Owen
I reproduced this just now by accident:
我刚刚无意中复制了这个:
lists the remote branches
git branch -r origin/Feature/f1234 origin/master
I want to checkout one locally, so I cut paste:
git checkout origin/Feature/f1234
Presto! Detached HEAD state
You are in 'detached HEAD' state. [...])
列出远程分支
git branch -r origin/Feature/f1234 origin/master
我想在本地结帐一个,所以我剪下粘贴:
git checkout origin/Feature/f1234
快!分离的 HEAD 状态
You are in 'detached HEAD' state. [...])
Solution #1:
解决方案#1:
Do not include origin/
at the front of my branch spec when checking it out:
检查时不要包含origin/
在我的分支规范的前面:
git checkout Feature/f1234
Solution #2:
解决方案#2:
Add -b
parameter which creates a local branch from the remote
添加-b
从远程创建本地分支的参数
git checkout -b origin/Feature/f1234
or
git checkout -b origin/Feature/f1234
或者
git checkout -b Feature/f1234
it will fall back to origin automatically
git checkout -b Feature/f1234
它会自动回落到原点
回答by André R.
try
尝试
git reflog
this gives you a history of how your HEAD and branch pointers where moved in the past.
这为您提供了过去如何移动 HEAD 和分支指针的历史记录。
e.g. :
例如:
88ea06b HEAD@{0}: checkout: moving from DEVELOPMENT to remotes/origin/SomeNiceFeature e47bf80 HEAD@{1}: pull origin DEVELOPMENT: Fast-forward
88ea06b HEAD@{0}:结帐:从 DEVELOPMENT 移动到 remotes/origin/SomeNiceFeature e47bf80 HEAD@{1}:拉原点 DEVELOPMENT:快进
the top of this list is one reasone one might encounter a DETACHED HEAD state ... checking out a remote tracking branch.
此列表的顶部是可能遇到 DETACHED HEAD 状态的一个原因……检查远程跟踪分支。
回答by Will
It can easily happen if you try to undo changes you've made by re-checking-out files and not quite getting the syntax right.
如果您尝试撤消通过重新签出文件所做的更改并且没有完全正确使用语法,则很容易发生这种情况。
You can look at the output of git log
- you could paste the tail of the log here since the last successful commit, and we could all see what you did. Or you could paste-bin it and ask nicely in #git
on freenode IRC.
您可以查看输出git log
- 您可以将自上次成功提交以来的日志尾部粘贴到此处,我们都可以看到您做了什么。或者你可以将它粘贴#git
到freenode IRC上并很好地询问。
回答by radzimir
It can happen if you have a tag named same as a branch.
如果您有一个与分支名称相同的标签,就会发生这种情况。
Example: if "release/0.1" is tag name, then
示例:如果“release/0.1”是标签名称,则
git checkout release/0.1
produces detached HEAD at "release/0.1". If you expect release/0.1 to be a branch name, then you get confused.
在“release/0.1”处产生分离的 HEAD。如果您希望 release/0.1 是一个分支名称,那么您会感到困惑。
回答by Tim Skov Jacobsen
Detached HEAD
means that what's currently checked out is not a local branch.
Detached HEAD
意味着当前签出的不是本地分支。
Some scenarios that will result in a Detached HEAD
state:
一些会导致Detached HEAD
状态的场景:
If you checkout a remote branch, say
origin/master
. This is a read-only branch. Thus, when creating a commit fromorigin/master
it will be free-floating, i.e. not connected to any branch.If you checkout a specific tag or commit. When doing a new commit from here, it will again be free-floating, i.e. not connected to any branch. Note that when a branchis checked out, new commits always gets automatically placed at the tip.
When you want to go back and checkout a specific commit or tag to start working from there, you could create a new branch originating from that commit and switch to it by
git checkout -b new_branch_name
. This will prevent theDetached HEAD
state as you now have a branch checked out and not a commit.
如果您签出远程分支,请说
origin/master
。这是一个只读分支。因此,从origin/master
它创建提交时将是自由浮动的,即不连接到任何分支。如果您签出特定标签或 commit。从这里进行新提交时,它将再次自由浮动,即不连接到任何分支。请注意,当一个分支被检出时,新的提交总是会自动放置在提示处。
当您想返回并检出特定提交或标记以从那里开始工作时,您可以创建一个源自该提交的新分支并通过
git checkout -b new_branch_name
. 这将阻止Detached HEAD
状态,因为您现在已检出分支而不是提交。
回答by Thomas Weller
A simple accidental way is to do a git checkout head
as a typo of HEAD
.
一个简单的意外方法是将 agit checkout head
作为HEAD
.
Try this:
尝试这个:
git init
touch Readme.md
git add Readme.md
git commit
git checkout head
which gives
这使
Note: checking out 'head'.
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.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at 9354043... Readme
回答by dspeyer
The other way to get in a git detached head state is to try to commit to a remote branch. Something like:
进入 git detached head 状态的另一种方法是尝试提交到远程分支。就像是:
git fetch
git checkout origin/foo
vi bar
git commit -a -m 'changed bar'
Note that if you do this, any further attempt to checkout origin/foo will drop you back into a detached head state!
请注意,如果您这样做,任何进一步尝试结帐 origin/foo 都会使您回到分离的头部状态!
The solution is to create your own local foo branch that tracks origin/foo, then optionally push.
解决方案是创建您自己的本地 foo 分支来跟踪 origin/foo,然后选择推送。
This probably has nothing to do with your original problem, but this page is high on the google hits for "git detached head" and this scenario is severely under-documented.
这可能与您的原始问题无关,但是此页面在“git detached head”的谷歌点击量中很高,并且这种情况的记录严重不足。
回答by Nesha Zoric
When you checkout to a commit git checkout <commit-hash>
or to a remote branch your HEAD will get detached and try to create a new commit on it.
当您检出提交git checkout <commit-hash>
或远程分支时,您的 HEAD 将分离并尝试在其上创建新提交。
Commits that are not reachable by any branch or tag will be garbage collected and removed from the repository after 30 days.
任何分支或标签无法访问的提交将在 30 天后被垃圾收集并从存储库中删除。
Another way to solve this is by creating a new branch for the newly created commit and checkout to it. git checkout -b <branch-name> <commit-hash>
解决此问题的另一种方法是为新创建的提交和检出创建一个新分支。 git checkout -b <branch-name> <commit-hash>
This article illustrates how you can get to detached HEADstate.
本文说明了如何进入分离的 HEAD状态。