A detached HEAD occurs when you check out a commit that is not a branch. The term detached HEAD
tells you that you are not viewing the HEAD of any repository. The HEAD is the most recent version of a branch. This is sometimes called the “tip of a branch”.
In this guide, we discuss what a detached HEAD is and how they work. We’ll walk through how to navigate into and out of a detached HEAD in Git.
What is a Detached HEAD?
Git stores a record of the state of all the files in the repository when you create a commit. Because Git has such advanced version tracking features, you can go back to any point in time in your repository to view its contents.
This is useful if you want to retrieve code you have overwritten in newer commits. You only need to check out the commit you want to view and then you can copy all the code you need from that commit. Being able to review past commits also lets you see how a repository or a particular file or set of files has evolved over time.
When you use the git checkout command to view a commit, you’ll enter “detached HEAD state”. This refers to when you are viewing a commit that is not the most recent commit in a repository.
Detached HEAD state is not an error nor is it a problem. When you are ready, you can navigate back to the HEAD in your repository.
Enter Into a Detached HEAD
We have a repository called ck-git
. We want to view the state of the README.md
file three commits back in our repository.
To do this, we can use the git checkout command:
git checkout HEAD~3
This command lets us see how our repository appeared three commits back. The ~3 tells Git we want to view our repository three commits relative to HEAD. HEAD is the most recent commit in a repository.
Let’s run the git checkout command:
Note: checking out ‘HEAD~3’.
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.
Git informs us with the above error message we have entered into a detached HEAD state.
Save Changes to a Detached HEAD
Once you enter into detached HEAD
state, you can view and modify the files in a particular commit. This is useful if you want to retrieve a change from a past commit that should be reincorporated into your repository.
To save a change from a detached HEAD, you need to create a new Git branch:
git branch dev
This lets us create a branch called dev
. dev
will show our repository three commits back.
Next, we need to merge our changes into the master branch. We can do this by navigating to the master branch and merging our two branches. We need to navigate to the master
branch because we are currently viewing a previous commit in the master
branch.
git checkout master git merge dev
The first command will navigate our user to the master branch. The second command will start a Git merge between the master
and the dev
branch.
Then, we can create a new Git commit with the changes we have collected from our previous ref HEADs. ref HEADs
are previous references, or commits, in our repository.
Alternatively, you can create a new branch using the git checkout -b command if you want to navigate to the dev
branch.
Discard Changes in a Detached HEAD
You do not need to save the changes you make to a detached HEAD.
Once you are done viewing a previous commit, you can go back to the HEAD of your repository. First, we need to discard the changes we made using the Git reset command:
git reset --hard
"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
This command will make sure there are no conflicts when we want to move back to our master branch. Next, we can check out the HEAD of our master branch using the checkout command:
git checkout master
This will change your current HEAD to the tip of the master branch. None of the changes you made in the detached HEAD state will be reflected on your repository.
Conclusion
A detached HEAD occurs when you are viewing a single commit in the history of a Git repository. You’ll see a message whenever you enter into detached HEAD state informing you that you are no longer on a branch.
Now you have the knowledge you need to use the Git detached HEAD state in your software development practices.
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.