Open In App

Git – Head

Last Updated : 08 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pre-requisites: Git

Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Git relies on the basis of distributed development of software where more than one developer may have access to the source code of a specific application and can modify changes to it that may be seen by other developers.

As we all know git allows a team of people to work together, all using the same files. And it helps the team cope with the confusion that tends to happen when multiple people are editing the same files. While working with Git, we have seen many times the term named HEAD appear in many places like in commits, pulls, etc. 

What is Git Head?

The most recent commit to the current checkout branch is indicated by the HEAD. A pointer to any reference, in a sense. The “current branch” can be thought of as the HEAD. The HEAD is moved to the new branch when you use the “checkout” command to swap branches.

Git Head architecture

 

Git Refs and Heads

HEAD is the reference to the most recent commit in the current branch. This means HEAD is just like a pointer that keeps track of the latest commit in your current branch. However, this definition just gives us a basic overview of HEAD, so before deep diving into HEAD let us learn about two things before that is refs and head.

When you initialize your local directory to a git repository, you see that a .git folder is created. Let us now create a file demo.txt and add this file to the staging area and commit the changes. 

Now let us get into the .git/refs folder, we can see two more subdirectories being created which are heads and tags. Inside the heads folder, you can find the branch names such as master, main, etc, and in tags such as v0.2, v0.3, etc. These are all examples of refs. 

When we talk about branch names in the heads folder, a ref such as master represents the tip (latest commit ID) on that branch and this is called head. In this folder, we can find different files for different branches and each file will represent the latest commit ID of that branch. So, if any of the new commits is made this file keeps track of the latest commits.

Master

 

Let us check what the master file consists of as follows:

We can see that our commit is being written in the master file, you can verify your commit ID using the git log command.

To conclude refs and heads are pointers to the latest commits, in the form of files where the file name represents the name of the branch and they contain the commit ID that the branch is pointing to. Similarly, if you want to see about the tags you can see them in the .git/refs/tags path. If you want to learn more about tags and what contents the files in the .git folder contain.

Now let us get back to our main question what is HEAD? 

Now that we have learned about refs and heads let us learn about HEAD. HEAD can be termed as a special ref that points to the current commit. However, HEAD can change depending on the latest commit that we have checked out in our git directory. We can check out our HEAD in the .git/HEAD file. Let us what our HEAD file consists of as depicted as follows:

ref

 

This is telling Git that the working directory is currently pointing to the master branch. If we create another branch then it would be pointing to that branch. Let us see that with the help of an example:

git branch

 

We can see that we have created a branch called main, and our HEAD is now pointing to the main branch. You can also check in your .git/refs/heads folder.

Branches

 

Let us now add more commits to our master branch and see where our HEAD points to.

Here we have added Hello Geeks in the demo.txt file.

git checkout master

 

Git Show Head

To verify the HEAD status use the below command and it will also show the HEAD location.

Syntax:

git show HEAD

Output:

git show head

 

From the above image, we can observe the commit ID is given by the HEAD. It means the HEAD is on the given commit.

lets us check the commit history/logs of the project by using the below command.

Syntax:

git log 

Output:

git log

 

We have two commits and our HEAD is now pointing to the most recent commit we have done. You can also check this in your .git/refs/heads” folder.

Commit ID

 

Git Detached Head

When a commit is currently checked out but not connected to a branch reference, this is referred to as a “detached HEAD” in Git. Instead of pointing to the branch that the commit belongs to, the HEAD pointer instead points directly to a particular commit.

You can change the pointing of HEAD using the command:

git checkout <commit ID>

We created two commits and our HEAD is pointing to the 2nd commit where we added “Hello Geeks” to our file, so let’s point our HEAD to the first commit that we created.

git checkout

 

We can see the last line which states that HEAD is now at 01cbf53 Created demo.txt.  Basically, it’s telling that HEAD is now pointing to commit 01cbf53. Let’s check our HEAD file in .git/HEAD. As soon as we type the git checkout command we can see that our working repository is now returned to the commit ID that we have put. You can check your demo.txt. It would be empty because, in the first commit, we just created demo.txt.

git ID

 

And Yes, we got the same commit ID that we were expecting. 

Note: Geeks, now you must be wondering why should we use HEAD.

As seen in the above example, going back to the previous commitment becomes easy and is quite useful in our working profession. The concept of detached HEAD is useful as we can view that file’s old version in context.

However, if we only want to revert that specific file, we can run the below command as follows:

git checkout <commit ID> <file>

Conclusion 

To sum up, “git head” refers to the branch’s most recent commit in a Git repository. It serves as a pointer to the branch’s tip and is updated as new commits are made to the branch. While they work on new changes based on the status of the codebase, developers utilize “git head” to maintain track of the most recent changes in their code. When dealing with Git, it is crucial to comprehend the idea of “git’s head” because it is an essential component of the Git workflow and version control system.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads