What Is HEAD in Git?

In git, HEAD is like a pointer that points to the local branch you're currently on. If, for example, you switch to a different branch, then HEAD would point to that branch. Internally, git keeps a reference of the "current" branch in .git/HEAD file (which you can see if you print the contents of it):

cat .git/HEAD

For example, if you're on the main branch, then it would output the following:

ref: refs/heads/main

If, for example, you switch to a branch called "foo", then the HEAD would point to the branch ref of that branch:

git switch foo

cat .git/HEAD

# output: ref: refs/heads/foo

Please note that when you switch to a certain commit, tag or remote branch, then the HEAD would be in a "detached" state.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.