How did I end up with a detached HEAD?

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


For recovering a detached-HEAD, you can :

git checkout master

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.