Even git has a HEAD…

So as you know, there is no human that can function properly without a head and as it turns out the world’s most popular version control system also has a head. Well not in the same sense as humans, but you’ll get the hang of it as I go on.

First of all you do remember that to push your files to your git server you need to go through the following steps:
1. Stage your files using the “git add <filename>” command.
2. Commit your files using “git commit -m ‘some comment’”.
3. Push your files using “git push origin master”

At the point of committing your files, git creates and object and stores the 
SHA-1 value of your current commit in a file named …. you guessed it … “HEAD”.

In technical terms, HEAD stores a symbolic reference to the last commit of the current branch. This means that if I am currently working in the master branch of my repository, HEAD will hold a reference to the last commit that I made in this branch.

cat .git/HEAD

If you were to run the above command, you will be greeted with a response that looks like this “ref: refs/heads/master”. This is the reference to the actual place where the SHA-1 is stored. Consequently if you run “cat .git/refs/heads/master” you would get a value that looks likes “6d31dccfbc36a5851e1cacb34b6a24aeb9af0527”. Don’t worry, you never have to memorize this value, its just something to help git keep track of your last commit.

In a situation where you need to revert to your last commit then feel free to use :

“git reset — hard HEAD”

This will cause the HEAD to go a step back and point to your last commit.

There are also some variations of HEAD which include:

ORIG_HEAD: Whenever you do a merge or reset, just before changing the value of HEAD is changed to point to the new commit, the previous commit reference is stored in the ORIG_HEAD. So if you ever need to revert to the previous commit you can obtain the reference from ORIG_HEAD like so
 “git reset — hard ORIG_HEAD”.

FETCH_HEAD: In a fetch operation, the head for the most recently fetched branch is stored in the FETCH_HEAD just immediately after the fetch operation.

MERGE_HEAD: In a merge operation between two branches, the head of the other branch is temporarily stored in the MERGE_HEAD just before the branches are merged into one commit.

Unfortunately that’s the much I can say about git’s HEAD for now. As soon as I find out more you can be sure I would update you. Until then, keep collaborating.

Like what you read? Give MICHAEL OGALA a round of applause.

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