What is HEAD in Git?
HEAD is a pointer to the commit you currently have checked out. Normally it points to a branch name, which points to that branch's latest commit, so HEAD marks where your next commit will be added.
Explain it simply
HEAD is the 'You are here' arrow on a railway map. The branches are named stations, and HEAD says which station you are standing at right now.
Usually HEAD does not point at a commit directly. It points at a branch such as main, and the branch points at its latest commit. When you commit, the branch moves forward and HEAD moves with it. Shortcuts such as HEAD~1 mean the parent of the current commit, and HEAD~3 means three commits back. The value is stored in the file .git/HEAD.
Example
cat .git/HEAD # ref: refs/heads/main
git log -1 HEAD # the commit you are on
git show HEAD~1 # the commit before it
git diff HEAD # working tree versus the last commitWhat to say in the interview
"HEAD is a reference to the currently checked-out commit, usually through a branch. When I commit, the branch advances and HEAD follows. I use HEAD~1 or HEAD~2 to refer to earlier commits, for example in git reset HEAD~1 or git diff HEAD."
Common mistakes
- Thinking HEAD is the same thing as the branch; it points to the branch, or directly to a commit when detached.