Git HEAD and its Importance

Eddy Karanja
2 min readAug 19, 2016

When working on a project, it’s important to track changes to your files and be able to refer to a certain change that you performed at a certain point. This is, without the jargon, what is known as version control.

There are many tools to help developers track changes to their projects but git happens to be one of, if not the most popular. It’s an open source project that was developed by the Linus Torvalds(the guy that created the Linux OS kernel) in 2005. It’s easy to integrate with other online repos such as Bitbucket, Github and Heroku.

The <HEAD>

One of the features I’d like to talk about is the Git HEAD. Whenever a [commit] is performed on a project, a snapshot In a nutshell, the HEAD is the reference to the current commit of your branch.

When a new branch is created, the HEAD does not move to the newly created branch. This is because Git points to your currently active commit and not the branch. To move branches, you have to ‘git checkout’.

Detatched HEAD:

When checking out a branch, Git moved the pointer (head) along when you create a new commit. However, if you check out a non-referenced you end up with a detatched head.

$ git init example
Initialized empty Git repository in example/.git
(master) $ touch file
(master) $ git add file
(master) $ git commit -m "Initial"
[master (root-commit) 123be6a] Initial
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 file
(master) $ touch other
(master) $ git add other
(master) $ git commit -m "Second"
[master 5a11d1c] Second
0 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 other
(master) $ git log --oneline
5a11d1c Second
123be6a Initial
(master) $ git checkout HEAD^
Note: checking out 'HEAD^'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

git checkout -b new_branch_name

HEAD is now at 123be6a... Initial
((123be6a...)) $ git log --oneline
123be6a Initial

So, why is the head important?
* It gives the developer ability to move back to a previous checkpoint in the project development.
* It helps keep track of commits.

--

--

Eddy Karanja

A techpreneur, software developer and fellow at Andela. I love to create through code and creative writing.