How did I end up with a detached HEAD?
Asked Answered
C

3

14

I checked out a commit/branch from master, and then checked out back to master and wrote something. After that, I committed it, but I ended up with a detached HEAD. Why?

Here is what I did:

  1. Create a new project and create git repository.
  2. git add
  3. git commit
  4. type some words
  5. git commit
  6. checkout to previous commit
  7. checkout back

    step7

  8. type some word

  9. try to commit; it prompts there is a detached head.

    step9

IntelliJ IDEA's console shows:

17:08:58.143: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:08:58.143: git init
Initialized empty Git repository in C:/Users/jiahao/IdeaProjects/testtt/src/.git/
17:09:16.331: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:16.331: git -c core.quotepath=false add --ignore-errors -- C.java
17:09:24.407: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:24.407: git -c core.quotepath=false commit --only -F C:\Users\jiahao\AppData\Local\Temp\git-commit-msg-0.txt -- C.java
[master (root-commit) 22d1c79] first commit
 1 file changed, 6 insertions(+)
 create mode 100644 C.java

17:09:38.060: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:38.060: git -c core.quotepath=false commit --only -F C:\Users\jiahao\AppData\Local\Temp\git-commit-msg-0.txt -- C.java
[master 69084f3] second commit
 1 file changed, 1 insertion(+)

17:09:44.136: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:44.136: git -c core.quotepath=false checkout 22d1c7919eab50925411d9bbb8a9ad1575608c27
Note: checking out '22d1c7919eab50925411d9bbb8a9ad1575608c27'.
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 22d1c79... first commit
17:09:46.576: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:09:46.576: git -c core.quotepath=false checkout 69084f344b79a48da92855d3fb633a28a672a302
Previous HEAD position was 22d1c79... first commit
HEAD is now at 69084f3... second commit
17:18:26.999: cd C:\Users\jiahao\IdeaProjects\testtt\src
17:18:26.999: git -c core.quotepath=false commit --only -F         C:\Users\jiahao\AppData\Local\Temp\git-commit-msg-0.txt -- C.java
[detached HEAD 783fbf2] third commit
1 file changed, 1 insertion(+)
Crat answered 25/1, 2016 at 8:22 Comment(8)
I suspect you didn't just checkout back to master. It would help if you could give a complete example (from git init onwards) of an exact set of steps to reproduce the problem.Mulch
No, that's not an exact set of steps - "checkout back" doesn't tell us exactly what you did. Ideally, do this from the command line to make it clearer than from an IDE.Mulch
Also show the results of git status from the command line.Mulch
Can you give us the lines in git reflog that are relevant?Cheltenham
When I use command line, trying to reproduce the problem, I fail because it works! So I think maybe it's IntelliJ IDEA's BUG. I will show you the reflog in IntelliJ IDEA later. Thanks.Crat
Whatever you did in IntelliJ ("checkout to previous commit") caused commands of the form git checkout <commit-sha> to be run, as shown by the log. Such a command checks out a commit rather than a branch, which puts you in detached-HEAD state.Sinkage
Yes when I checkout to previous commit it will put me in detached-HEAD state, but after that I checkout to master, and , in my opinion, I will not be in detached-HEAD state anymore. Am I wrong? However, the fact is that after I checkout to master, I am still in detached-HEAD state.Crat
More info here:stackoverflow.com/questions/34519665/…Corneliuscornell
S
28

Problem and solution

Close inspection of the IntelliJ-IDEA log reveals the nature of the problem. After creating your first two commits, you were in the following situation:

enter image description here

Somehow, you checked out your first commit, which put you in detached-HEAD state:

enter image description here

Then, similarly, you checked out your second commit (which happened to be the tip of your master branch). This still left you in detached-HEAD state:

enter image description here


You write:

I checked out a commit/branch from master [...]

Be careful. Checking out a commit that happens to be the tip of a branch is not equivalent to checking out that branch!


Note that HEAD is now pointing directly to a commit, not to a branch. That is the definition of "detached HEAD". The fact that HEAD points to the same commit as master changes nothing about the fact that your HEAD is detached.


Some IDEs may not give you a clear indication that you are in fact in detached-HEAD state. Even git log --decorate, for a long time, gave you no clue as to whether HEAD was pointing to master, or detached and pointing directly at master's tip.


You then made a third commit, which, as expected, still left you with a detached HEAD; your master branch still points to the second commit.

enter image description here

To get out of detached HEAD-state, you need to reattach HEAD to a branch (master, here). How depends on what you want to do. In the following, I'm assuming you have access to the Git CLI:

  • If you want to discard your third commit, simply run

    git checkout master
    

    and you'll end up back in this situation:

    enter image description here

  • If you want to keep your third commit and make master point to it, run

    git branch -f master HEAD
    

    enter image description here

    and then

     git checkout master
    

    enter image description here

Parting tip

Learning Git in an IDE is a recipe for disaster. I recommend you build your understanding at the command line first, and only then start using Git from within a GUI, if you feel that doing so would improve your workflow.

Sinkage answered 25/1, 2016 at 13:46 Comment(1)
"Be careful. Checking out a commit that happens to be the tip of a branch is not equivalent to checking out that branch!" ... delivered a real moment of clarity for my 60yo brain there! I'm just learning, but have to say I've found the intellij UI helpful in that I have at least got started with making some use of Git, after several attempts at doing that via the CLI. Was always baffled by the complexity vs the minimal rewards you see as a solo dev.Law
P
3

I accidentally ran into detached-HEAD while developing a Java Spring project using IntelliJ 2020.2. This is how i got back to normal.

In git tool window [Alt+9], branch "Log: origin/master", had 3 labels: yellow = HEAD, green = master, violet = origin/master: label colors in git tool window

'HEAD' and 'origin/master' were applied to the latest commit enter image description here 'master' was applied to an earlier commit enter image description here.

To get out of detached-head and reset it to the desired state 'HEAD & master & origin/master all applied to branch's latest commit', right-clicked 'master' node 'Remote' list (located left to the branch's history under Log) and chose 'Checkout' enter image description here

Such got back to regular state in latest commit in branch enter image description here

Peculiar answered 23/4, 2021 at 14:27 Comment(0)
M
2

For recovering a detached-HEAD, you can :

git checkout master
Marcie answered 25/1, 2016 at 8:49 Comment(1)
This does not fix the current branchSalon

© 2022 - 2024 — McMap. All rights reserved.