Git Head

Learn via video courses
Topics Covered

Git, a powerful open-source version control system, is fundamental for collaborative software development. Central to Git's functionality is the concept of the "HEAD" - a reference to the latest commit in the active branch, crucial for tracking changes and managing multiple versions.

This guide explores the HEAD pointer, explaining its significance in Git's distributed environment where teams simultaneously edit and manage files. Understand how Git marks the active branch using .git/HEAD, a critical internal file.

Introduction to Git Head

Git Refs and Git Heads

Git refs and heads are nothing but pointers to the commits. These are stored in the form of files. refs and heads store the commit ID pointing to the branch.

We are using an existing Git repository to understand Git refs and Git heads. The below-given repository contains 2 branches master and newBranch. These are heads and they contain the latest commit ID made on the branch. We are currently on the master branch. It also has four files and a few existing commits as shown below.

Git Refs and Head

When we initialize a directory as a Git repository, Git creates a .git file inside the directory and this file is hidden. It stores the version information of the project such as commits, modifications, tags, etc.

Let us dig into the .git folder. We will use the cd command to enter the folder, cd .. to exit the folder, and ls to list the contents of the folder.

Git Heads and Refs

The .git folder contains another folder called refs; inside this ref is a folder heads with two files master and newBranch. These are nothing but the branches of our repository. Another folder in refs is the tags. Git tags are like bookmarks in the repository used to mark the version of the software. And the tags of the repository are listed here.

To explore the inner structure of the .git folder it is recommended to clone a git repository from GitHub and dig into the .git folder.

What is Git HEAD?

Git HEAD is a special ref, it points to the latest commit in the repository. It is also called the current or active branch. We can move this pointer to a different commit or branch by using the git checkout command.

Let us now visit the file HEAD in the .git folder. This file contains a reference to the master branch inside the ref folder.

Let us see what happens when we create a new branch temp.

We have created a new branch called temp and also a new file called branch2.

Now the .git/refs/HEAD file contains the pointer to the temp branch.

Let us also try making some commits and checking the position of the HEAD.

As we know that the HEAD is pointing to the temp branch, let us check out the file .git/refs/heads/temp.

It points to the latest commit made in the current branch.

The latest commit made was adding the second file. Its commit ID is 7253ffd36f1a2964b70b1235dea41c64d99c12a7 and so is the content of the temp file present in the head folder of the refs folder.

When Should You Use Git HEAD?

The HEAD in git allows us to return to the previous commits and the previous state of the project. This lets us make changes in the previous stages of the project. It is very useful while developing projects in a team.

We can use git checkout on tags or commit IDs to re-visit them. This detaches HEAD back to the previous commit and the file can be viewed in the older version. Before making any major changes here, create a branch using git branch <branchname> to save the changes.

If these changes are necessary, the git commit and git merge commands can be used to apply the changes.

Git Show Head

The git show HEAD command returns the details latest commit or the latest checked-out commit. It is a quick way to get HEAD details such as commit message, short commit ID, etc.

Syntax:

The below example shows an example output of the above-given command.

The git show HEAD command displays the commit ID our HEAD is currently pointing to along with the commit message and the changes made in that particular commit.

Git Detached Head

Detached HEAD is a state of the repository where the HEAD pointer is not pointing to the latest commit in the repository. HEAD is made to point to the latest commit, but when it doesn't it's called DETACHED HEAD.

Fixing Detached HEAD

To fix the problem of Detached HEAD, two possible ways are available.

The first method

The first method to resolve Detached HEAD, is using the git checkout command.

To detach a head we are checking out to the master branch. The changes in the temp branch are not merged into the master branch yet.

Git shows a warning about the detached HEAD. Look at the log below, it no more contains commits made on the temp branch.

To undo this operation, we can use the checkout operation. It is only possible to check out this commit if we know the commit ID or the branch name. As we can see our old commits are derived back.

It is also possible to get the changes on the Detached HEAD back if we are aware of the branch name. If we run git checkout temp will run the same way.

The second method

In case you wish to build a part of the program on the previous commit, you can create a new branch and solve the issue of the Detached HEAD.

The Changes made to the Detached HEAD are lost if we move back to the master branch or the latest commit. To avoid such data loss, creating a new branch and working on it is always safe.

This will save the changes to a newly created branch and the head is also now pointing to the latest commit. After the work is done, the branch can be merged back into the parent branch.

Git HEAD vs Head

Every branch has the latest commit referenced by a head(lowercase). The branches file that we have in the .git/refs/head folder contains files named with branch names that have the commit ID of the latest commits called heads. The HEAD (capital HEAD) is the reference to a commit we are currently viewing it.

Conclusion

  • Git is an open-source, free version control system used to work on projects in teams.
  • Git HEAD can be thought of as a pointer that keeps track of the latest commit in the repository.
  • Inside the .git folder there exits a folder refs. It further contains 2 directories heads and tags. The head folder contains the references or commit IDs of all the branches in the Git repository. The tags folder contains all the tags along with their references initialized in the project.
  • The .git folder also contains a file HEADS containing the latest commit's pointer or commit ID.
  • The HEAD pointer can be moved to different commits and branches using the git checkout command. In this case, it is not the latest commit, and thus the HEAD is now called Detached HEAD.