THE ‘HEAD’ IN GIT

HEAD is basically a pointer (a reference) to the current branch. It’s a snapshot of the last commit performed in a working directory. Other than serving a pointer to the last commit performed, the HEAD also serves as a parent for the next commit to be performed.

In order for one to see the current snapshot of their HEAD (where the HEAD is pointing), this is the command that you should use inside your local Git initialized repository via the Git Bash terminal:

$ cat .git/HEAD

In my case my output looks like this

From the above image we can see that the HEAD is currently pointing (referencing) to the ‘master’ branch. i.e

ref: refs/heads/master

The above reference (pointer) will also serve as a parent for my next commit in the ‘master’ branch.

Detached HEAD

I bet you didn’t see this coming but we also have detached HEADS. Awh yeah!

A detached HEAD is achieved if you checkout a non-referenced HEAD (referencing a non-existent parent branch).If you’re confused don’t worry. I’ll get down to it shortly.

Detached HEAD’s are a rare occurrence but they do happen. Here is an example:

1. Basically what we’ve done here is we created a local bootcamp-10 repository and initialized it

2. We also created, added two dummy files and committed them to our ‘master’ branch

3. We then performed a git checkout HEAD^ command (which basically moved one step back to see who is the ‘Father’ of ‘master’ our branch).Unfortuately, our ‘master’ branch didn’t have a ‘Father’ (parent)so we end up with a detached HEAD with some random number(see bottom line)

the last line is an indication that our HEAD is detached

Your comments and suggestions are welcome regarding this article.Remember to Share if you found it resourceful

Like what you read? Give Mike a round of applause.

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