Git Head. What is it?
What is git?
Git is a distributed version system developed by Linus Torvalds to address the shortcomings and speed issues in the version control systems of his time. Git is preferred by a lot of development companies around the world as it is easy to learn and provides a huge range of features. One of the many, important and usually misunderstood features of git is the HEAD pointer. The HEAD, largely unknown especially in Nigeria, plays a significant role in the git system and I would be writing about it in this post.
What is the git head?
Reading through the official git documentation, git reference and Stack Overflow. I found the following quotes and attempts to define the HEAD. First definition from StackOverflow,
Head is simply a reference to a commit object. Each head has a name (branch name or tag name, etc). By default, there is a head in every repository called master.”. -http://stackoverflow.com/a/4381549/6121313
The second definition also Stack Overflow,
HEAD is (direct or indirect, i.e. symbolic) reference to the current commit. It is a commit that you have checked in the working directory (unless you made some changes, or equivalent), and it is a commit on top of which “git commit” would make a new one. Usually HEAD is symbolic reference to some other named branch; this branch is currently checked out branch, or current branch. — http://stackoverflow.com/a/967611/6121313
The HEAD file is a symbolic reference to the branch you’re currently on. By symbolic reference, we mean that unlike a normal reference, it doesn’t generally contain an SHA-1 value but rather a pointer to another reference. — https://git-scm.com/book/en/v2/Git-Internals-Git-References#The-HEAD
From above, git HEAD is the pointer to either the head of the current branch or the present checked out commit. Unlike git ref which refers to a commit’s hash, the HEAD only points to the currently checked out commit at any given moment. Giving the example of a repository with two branches ‘master’ and ‘test’, with ‘master’ currently checked out. Running the command on git bash
will produce the result
using the same command on the “test” branch will produce the result
The result validate the points from above that the HEAD is a pointer to the present branch head commit. If we check out a commit from the present branch, git enters a DETACHED HEAD state which means the HEAD is presently detached and pointing to a temporary branch with its head being the currently checked out commit. The HEAD can be reattached by checking out to either a new branch from the commit using
or checking out to any existing branch.
Why is it important?
Git’s HEAD is crucial to the working on git in that it is a very convenient way to show what the user is presently working or in git term what the user has checked out. The git HEAD provides the convenience of not having to remember or know the last checked out branch or commit manually as the information is automatically pointed to and stored by the git application.
HEAD pointer makes it easy to move back to a previous commit in any branch by pointing to the hash of the commit. This makes it possible to rollback, reverse or revert to an earlier version if there has been some error or prior version of the software is preferred more.