wilson staley

Git Detached Head

Today we are going to look at what it means to be in a detached head state in a git repo. While "Detached Head" sounds pretty scary, it is actually not bad at all and is a pretty helpful thing to understand for a developer who wants to travel back in time to view a project in an earlier state.

Let's begin by understanding what the HEAD reference is in Git...

"HEAD" is essentially a pointer to a commit that represents what point in a project's history you are viewing. HEAD normally references a commit indirectly through a branch reference. Let's look at the diagram below to get a better understanding of how this works.

git diagram

As you can see in this example, HEAD is referencing the "main" branch, which references a commit. So when you use a command like git switch my-branch, HEAD would simply jump to a different branch reference.

However, Git does not restrict you from only switching to and from branches! With Git, you can actually checkout a particular commit and view the project at any point in time throughout its history. To do this, simply use the command git checkout <commit hash>. Note, you can view your project's history of commits by using the git log command. This will show you a list of commits with their associated commit hashes.

💡

To checkout a commit, you only need to use the first 7 characters of the commit hash. You can use the command git log --oneline to see an abbreviated history of commits and commit hashes.

So, in the example above if I wanted to see what the state of the project was at the second commit, I could run git checkout eddd976.

Time Travel Unlocked! 🔓

After running git checkout eddd976, our diagram would look like this:

git detached head

This is what's known as a detached HEAD, because HEAD is no longer pointing to a branch reference, but is pointing directly to a commit. Once you're in a detached HEAD state, you can do a couple things...

The first thing you can do is just look around. Sometimes all you want to do is look at the project at a certain point in time to see how something was done, copy some code, or reminisce. When you're done looking around in a detached HEAD state, you can simply run git switch <branch name> to return HEAD to a branch reference and exit a detached HEAD state.

You also have the option to create a new branch from a detached HEAD state. Perhaps you wanted to return to a previous point in your project's history and try redoing something. You could accomplish this by first checking out a commit you want to use as a starting point, then creating a new branch and committing to the new branch.

Let's create a new branch while in a detached HEAD state using git switch -c <branch name>. In our diagram if we were to run git switch -c new-branch, we would get the following:

git new branch
💡

Any time you create a new branch, the starting point will be based off of HEAD.

Once you have created a new branch, you are no longer in a detached HEAD state because HEAD is now pointing to a branch reference! From here you can continue on creating commits just like you would on any other branch:

git new branch commit

And that's all there is to it! Hopefully this helped you understand what "HEAD" is in Git, and how entering a detached HEAD state can actually be quite useful.

Resources

If you are looking to learn more about Git and Github, I would highly recommend Colt Steele's Git & Github Bootcamp! I am currently taking this course and am learning a lot!