Dealing With “Detached HEAD” State in Git

When you pull in branches from your repository remote or upstream by running say git pull origin master (to pull changes from origin master branch) or git pull upstream non-breaking-space to pull in non-breaking-space branch from upstream; checking out any of these remote branch (e.g. git checkout upstream/non-breaking-space) will result in detached HEAD state because they aren’t proper branches as they do not (currently) exist locally.

$ git checkout upstream/non-breaking-space
Note: checking out 'upstream/non-breaking-space'.

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 b1a3ef5... Issue #29 Fix bug with non-breaking spaces

In a nutshell, detached HEAD state occurs when you try to checkout something that is not a local branch. It can be a commit, a tag or a remote branch.

when you make changes and commit them, these changes do not belong to any branch but that doesn’t mean the commits are deleted if you eventually switch to another branch.

As displayed in the command line interface, you have to check out a new branch to retain any changes and commits made like so:

git checkout -b new_branch_name

Say you created a w3guy.md, staged and committed the change. And then switch to branch SupportElements.

echo 'hello w3guy readers' > w3guy.md

git add w3guy.md

git commit -am "commit made in detach head state"

git checkout SupportElements

To include the commit we made in detached head state above to our current branch; firstly, the SHA-1 checksum of the commit has to be known.

If you don’t know it, run git reflog show HEAD@{now} -10 to list ten revision history of HEAD.

$ git reflog show HEAD@{now} -10
1b990a8 HEAD@{Sat May 28 22:24:06 2016 +0100}: commit: commit made in detach head state.
b1a3ef5 HEAD@{Sat May 28 22:20:24 2016 +0100}: checkout: moving from SupportElement
5845d85 HEAD@{Sat May 28 22:18:19 2016 +0100}: checkout: moving from SupportElement
5845d85 HEAD@{Sat May 28 22:01:10 2016 +0100}: checkout: moving from b1a3ef5cbfb
b1a3ef5 HEAD@{Sat May 28 21:12:46 2016 +0100}: checkout: moving from hum3 to ups
b1a3ef5 HEAD@{Sat May 28 20:52:14 2016 +0100}: checkout: moving from b1a3ef5cbfb
b1a3ef5 HEAD@{Sat May 28 20:51:34 2016 +0100}: checkout: moving from SupportElement
5845d85 HEAD@{Sat May 28 20:31:35 2016 +0100}: checkout: moving from master to SupportElement
38992da HEAD@{Sat May 28 19:53:37 2016 +0100}: checkout: moving from hum to master
45a6b74 HEAD@{Sat May 28 19:48:06 2016 +0100}: checkout: moving from master to hum3

Note: HEAD is actually a special type of reference that points to another reference. It may point to master or it may not (it will point to whichever branch is currently checked out).

With the SHA-1 checksum of the commit known, run git cherry-pick 1b990a8 to import or rebase the commit on the branch.

I wrote about cherry-pick in a previous post. You might want to check it out.

La Fin!

Don’t miss out!
Subscribe to My Newsletter
Invalid email address