Skip to main content
X

Explore your training options in 10 minutes

Git Tutorials

Git Detached HEAD

Felipe Bohórquez - December 29, 2020


While you might not encounter the problem of a detached HEAD, it is important to know about it so that you can avoid it. As a recommendation do not commit on a detached HEAD but if you do, we’ll show you how to save your commits.

A Reminder

In order to understand what the HEAD is, let’s do a refresher on Git’s underlying tree-like structure.

  1. The Working Directory: Or working tree. It refers to your local directory and git status will give you the state of your working directory.
  2. HEAD: It is just your current branch last commit snapshot. If you were to switch branches with git checkout then the HEAD will change to the last commit on the branch.
  3. Index: Or staging area, so when you git add files to commit it adds them to this index.

Understanding the Attached HEAD

Now that we understand that HEAD is just the latest commit on the current branch you are on. By doing git status it will tell your branch e.g. On branch master and by doing git log it will tell your commit history with information such as:

Get offers and scholarships from top coding schools illustration

Find Your Bootcamp Match

  • Career Karma matches you with top tech bootcamps
  • Access exclusive scholarships and prep courses










By continuing you agree to our Terms of Service and Privacy Policy , and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.

commit 38373004b8f651b58cea64cd629e1e2c18c164a0 (HEAD -> master, origin/master, origin/HEAD)
Author: Felipe <email>
Date:   Wed Sep 29 22:57:59 2020 -0500

Then if we were to change our branch to let’s say development branch with git checkout development then the HEAD will move to the last commit. So in all these normal cases the head is following us as it is supposed to be attached to a last commit on the branch we are on.

Detaching the HEAD

There are a couple of ways we can detach our HEAD.

  • Using the git checkout --detach command.
  • Checkout to a commit hash. E.g. If we use the commit from above git checkout 38373004b8f651b58cea64cd629e1e2c18c164a0
  • By adding ^0 on any given branch. E.g. git checkout master^0 .

I’m not sure whether you’d like to intentionally detach HEAD. So the most common case is that by mistake you wanted to checkout to a branch and used a commit hash instead of the branch name.

So what happens after a detached HEAD? You might get a warning similar to this:

$ git checkout 38373004b8f651b58cea64cd629e1e2c18c164a0
Note: checking out '38373004b8f651b58cea64cd629e1e2c18c164a0'.

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.

Once on a detached HEAD environment, if you do git status even though you made changes, it will say something like this:

HEAD detached at ac63806
nothing to commit, working tree clean

So it will be like you are frozen in time and everything you do is kind of lost, or merely not going anywhere. Why? Well because the HEAD is not pointing to anything.

Some people find this useful to explore a previous state of the repository without any worries of breaking things.

Re-Attaching the HEAD

You must understand that any of your branches will not be affected if you ever get into a detached state . Now, the best way to reattach the HEAD is to create a new branch.

We can do it as simple as git checkout -b <branch-name> .

What if we didn’t realize we were without a HEAD and started making changes? Well, here we will need to create a temporary branch and just merge with the branch we need to commit. For example:

git checkout -b temp-branch
git checkout master
git merge temp-branch

This will commit the changes from your temporary branch into the branch you need them. In this case master.

Venus, a software engineer at Rockbot

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

Conclusion

Today we learned how to detach our HEAD (not literally) and what to do afterwards. Understanding what the HEAD is in the Git inner workings can help us understand special cases such as a detached HEAD.

We also learned to not worry if we ever end up in a detached HEAD state.  Just do have in mind that since your HEAD is loose, changes and commits will not be reflected anywhere. So make sure you reattached the HEAD accordingly.

About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.

What's Next?

Felipe Bohórquez

About the author: Felipe Bohórquez is a Software Engineer and technical writer at Career Karma. He covers all things frontend and backend development.

Skip to main content