Version Control -:Git HEAD and its importance.

Andela Bootcamp and Git logo

Are you an aspiring software developer or a Tech geek, who has never heard about or used the tool called Version Control System (VCS)? If yes, hmmm, so weird! Not to get discouraged, I was once like you, thanks to Andela Bootcamp XVIb’s Software Trainer named Prosper. Briefly about what a VCS is. A Version Control System as defined by Github (a web-based Git repository hosting service ) is a system that records changes to a file or set of files over time so that you can recall specific versions later.

As are a graphic or web designer and want to keep every version of an image or layout (which you would most certainly want to), a Version Control System (VCS) is right tool to use. VCS allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. Few common examples of VCS: are CVS, Subversion, Git, Mercurial, Bazaar and LibreSource.

What is a Head? What a question right? Yeah, not Human head this time, Head as regards to Version Control Systems.

HEAD is Git’s (Git, a Version Control System Software) way of referring to the current snapshot. The HEAD in Git is the pointer to the current branch reference, which is in turn a pointer to the last commit you made or the last commit that was checked out into your working directory. That also means it will be the parent of the next commit you do. It’s generally simplest to think of it as HEAD is the snapshot of your last commit.

Example of a snapshot

Head can sometimes be called tip, that is, the most recent commit, either to the trunk or to a branch. The trunk and each branch have their own head, though HEAD is sometimes loosely used to refer to the trunk. but note that if a new branch is based on HEAD, then topologically HEAD is no longer a tip, since it has a child.

Git HEAD Example

Let’s start with the example git repository, assuming we have 2 commits in the repository. So how does git know which is the current commit for a command like git log?

Git uses the HEAD variable, which by default, is a reference to the current (most recent) commit. In our example, the most recent commit is labeled H1 in the diagram


Adding Commits Changes HEAD

Let’s add one more commit. The commits in the repository now look like the picture below, labelled H2.


Digging around more with git show and HEAD

The git show command reports the changes introduced by the most recent commit

Many git commands, such as git log and git show use HEAD as the commit to report on. However, you can specify a different the parent commit for git show to report on by using the ~ character (“tilde”).

There are quite a few different ways git allows you to specify which commit you would like to refer to in addition to the hash or using HEAD.

Behind The Scenes: Where Is The HEAD?

The contents of the git HEAD variable is stored in a text file in the .git/HEAD:

$ cat .git/HEAD
ref: refs/heads/master

That is telling us that we need to look at the file refs/heads/master in the .git directory to find out where HEAD points:

$ cat .git/refs/heads/master


HEAD mainly determines what branch you are serves as symbolic reference usually pointing to the branch which is currently checked out.

Note this difference: a “head” (lowercase) refers to any one of the named heads in the repository; “HEAD” (uppercase) refers exclusively to the currently active head. This distinction is used frequently in Git documentation.

#This is Paul!


#TIA-This is Andela!!!

Like what you read? Give Paul Oluyege a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.