What is HEAD in Git?
HEAD in Git is a pointer to the current place in the commit history, that is, to the commit you are currently working with.
Simply put: HEAD shows "where you currently are" in the repository.
What exactly HEAD points to
In a normal situation:
- HEAD points to the current branch
- the branch points to the last commit
- so HEAD indirectly points to the last commit of that branch
The chain looks like this:
HEAD → main → last commit
Understanding it with an example
If you are on the main branch and make a new commit:
- the commit is added to the end of the branch
- the
mainbranch moves forward - HEAD stays on the
mainbranch
Detached HEAD
Sometimes HEAD can point not to a branch, but directly to a commit. This is called detached HEAD.
For example:
git checkout a1b2c3dIn this case:
HEAD → commit a1b2c3d
Important for an interview:
- commits made in detached HEAD do not belong to a branch
- if you do not create a branch, they can easily be lost
Why HEAD is needed at all
HEAD is needed so that Git knows:
- which state to take files from
- where to add new commits
- which version of the project to show in the working directory
How to see HEAD
git statusShows the current branch (so that is where HEAD points).
git log --oneline --decorateShows where HEAD and the branches are in the history.
A frequent candidate mistake
Wrong: "HEAD is the last commit" Correct: HEAD is a pointer that usually points to the last commit of the current branch, but not always.
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.