Git Tutorial 18 – What Is A Detached Head In Git?

Introduction

As a part of GIT Tutorials End To End, we will learn about the Detached HEAD concept of Git in this post.

Prerequisite posts

I will expect that you are aware of the basic concepts and commands of GIT. But if you are a beginner in GIT then I strongly recommend you to refer GIT Basic Commands and Concepts section on my blog first.

Did you know that I have started a YouTube channel as well and I need your support to make it successful. Please do watch content then comment, like, share, and obviously subscribe.

I will also strongly recommend you to Lean about HEAD in git first to understand the detached head properly.

Clone a dummy repo

Let’s clone one of my public repo to practice and learn easily. The same repo I have used in my previous posts. In this repo, I have only the default branch “main” and three commits.

We can represent the above status of the repo using the below diagram-

Checking out a commit directly – Entry to detached head mode

We know that a branch is a pointer to a commit as we can see above. If we add a new commit in a branch then the branch will point to a new commit and HEAD will be updated as well to point to the new commit. This is an ideal situation.

We also know that a commit is a point that shows the status of your repo at that point. With every commit, your repo will have an updated version. Git provides us to jump to any commit directly. Remember when we switch to any specific commit then you will see repo status as per that commit only.

To checkout a specific commit we can use the below command-

git checkout <commitID>

We can use some starting characters of a commit id instead of a complete commit id. You can use git log –oneline command to see short commit ids.

Before checking out a specific commit

Checkout first commit

Now you are in a detached head. Now you are not in any branch but in a specific commit and HEAD is no more pointing to a branch name like ideal condition.

From the above command, you can see that you are in a specific commit now and HEAD is moved to that commit instead of the main branch. We have created a file FileA as part of the first commit and the same state we can see now. When we move into a detached head state, git let us know with all possible options. You can modify the repo state in a detached head.

Existing from detached mode

You can get out of a detached head state anytime by switching to a branch. You can use “git switch <branchName>” or “git checkout <branchName>” to switch to a branch. Please note git switch is a newer command introduced in Git 2.23 version.

Making changes in detached head mode and merge

Let’s check out to first commit and add and commit files.

To keep commits in the detached mode we need to create a new branch. You can use the git checkout command to create and switch to it as well as discussed here.

We can see we are no more in a detached HEAD.

Now you can merge the new branch to the main branch.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe to my YouTube channel.
#ThanksForReading
#HappyLearning

Leave a Reply

Your email address will not be published. Required fields are marked *