Git Tutorial 17 – What Is Head In Git?


As a part of GIT Tutorials End To End, we will learn about the 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.

What is GIT HEAD?

We have seen the word “HEAD” in our previous posts but I was holding on this topic for a suitable time to make you understand better. Today is the time.

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 two commits.

Observe the output of the git log command. It shows we have two commits in the repo. Newer commits are shown first. We also see HEAD -> main, origin/main, origin/HEAD with the second commit.

We know that when we clone a repo we get a hidden folder .git as well. It is the same folder when we run the git init command. This folder shows that a directory is a git directory and we can run git commands. Let’s go inside the .git folder and see its contents.

As soon as we changed the directory to the .git folder, the main changed to FIT_DIR!. If you remember it happens when we switch branches as shown in previous posts. Here it changed to just let you know that we are inside .git folder. The .git folder contains multiple files, folders and directories. You can see them explicitly in the file directory as well.

Ignore other files as of now and focus on a file named “HEAD”. Let’s see what is inside it. You can use UNIX command or explicitly open the file and see contents.

The HEAD file contains “ref: refs/heads/main“. In fact, it is storing a reference of another file which can be found at ref: refs/heads/main. “Main” is the currently checked out branch name here. If you switch to another branch then HEAD will contain ref: refs/heads/<branchName>“. We have another directory named ref inside .git folder.

Let’s go to the files pointing by HEAD i.e. refs/heads/main.

If you are good at Unix commands then we can use the below command instead of switching directory by directory.

We see some alphanumeric content here. Actually, it is nothing but the latest i.e. second commit id.

So HEAD indirectly points to the latest commit. If we create a new commit then HEAD will point to the latest commit.

There are multiple points to understand in the above commands.

  1. We can see the content of HEAD file is unchanged but its referenced file has latest commit i.e. 3rd commit.
  2. Observe output of git log command. Latest commit (3rd) shows HEAD -> main and just previous commit (2nd) shows origin/main, origin/HEAD. Earlier this commit i.e when we freshly cloned we saw all three together. Refer first screenshot in this post.
  3. When we cloned the repo, a local branch main which is same name as remote branch, is also created. At that time both local and remote branch was poniting to the same HEAD i.e. commit. With a new commit in local, HEAD of local main branch gets updated while HEAD of remote branch remained same old one as we have not puhsed our local change to remote repo.

Let’s push the new commit to remote and you can see HEADs of both local and remote are the same.

We can also use the git show HEAD command to see to which commit HEAD is pointing. It also shows the commit message and changes as part of the commit.

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.

Leave a Reply

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