The HEAD — An Exploration of Git

Git is a distributed Version Control System. Described technically, a version control system is a repository of files. The logic of Version Control Systems is based on the concept of tracking changes that happen within the respective files. These files are more often than not computer source code.

Like other control systems, the main objective of Git is to track changes made to the files inside the repository. Tracking these changes involves documenting not only the change, but who made it, why they made it and a brief on what problems making the changes fixed; or how making the changes enhanced the respective file. The significance of this with regard to software development is that it allows for not only proper documentation of changes and the saving of different versions of a software, but that it facilitates collaboration by multiple developers on one project; regardless of whether they are together or countries apart.

Git is made up of many different aspects, the HEAD being one of them. This article explores what the HEAD is, why it is important, and how it works.

What exactly is the HEAD?

In Git, references are files that store the location, of where commits are done. The files contain SHA-1 values stored under simple names, which makes it easier to locate the history of previous commits; as opposed to if you were using the SHA-1 values themselves. These files are stored in the ‘.git/refs’ sub-directory. The HEAD is one of these references.

The HEAD may be described as a reference to the currently checked out commit. By default, it is a symbolic reference to the branch that you have checked out. Symbolic because the HEAD, unlike a normal reference, does not contain an actual reference but instead a pointer to another reference. In simpler terms, the HEAD may be viewed as a reference to the tip of current branch. Where the latest commit is the tip. The other option, other than the default; for the HEAD, is when it is detached.

Why is the HEAD important?

The HEAD is important because through it we are able to keep track of our commits.

How does it work?

When the ‘git commit’ command is ran, it creates the commit object, specifying the parent of that commit object to be the SHA-1 value that the reference in the HEAD points to. Which means that the HEAD can be thought of as the commit branch. Running the ‘git show HEAD’ command shows you where the HEAD is currently pointing to (which is the last commit).

To see the HEAD of the previous commit simply do ‘git show HEAD~’ or ‘git show HEAD~~’ like below.

Switching branches can be done using the ‘git checkout <branch-name>’ command which simply switches the HEAD to point to the new branch. This makes it possible to commit to the new branch.

Detached Head

A detached head is what occurs when a specific commit is checked out instead of a branch.

The above shows a ‘git checkout’ to a previous commit’s SHA-1 value. To get out of this detached state, you can simply ‘git checkout master’ to go back to the master branch.