What is HEAD in Git?
Learn what HEAD means in Git, how it points to the current branch, what detached HEAD is, and how relative refs like HEAD~1 work in practice.
Expected Interview Answer
HEAD is a special pointer that tells Git which commit you currently have checked out; normally it points at a branch name, which in turn points at a commit, so HEAD indirectly tracks the tip of whatever branch you're on.
Concretely, HEAD is stored as a tiny file at `.git/HEAD`, and in the normal case it contains a symbolic reference like `ref: refs/heads/main`, meaning HEAD follows the `main` branch pointer wherever it moves. When you commit, Git creates the new commit, moves the branch that HEAD points to forward, and HEAD automatically follows since it's just referencing the branch, not a fixed commit. If you check out a specific commit hash or a tag directly instead of a branch, HEAD stores that commit's hash literally, a state called 'detached HEAD,' where new commits aren't attached to any branch and can be lost once you switch away unless you create a branch from them. HEAD is also used relatively in commands, `HEAD~1` means one commit before HEAD, `HEAD^` means its first parent, which is how commands like `git reset HEAD~1` navigate history without typing full hashes.
- Always tells you exactly which commit your working directory reflects
- Automatically follows the current branch as you commit
- Enables relative history navigation like `HEAD~1` and `HEAD^`
- Makes detached-HEAD experimentation possible without touching a branch
- Central to how switch, reset, and rebase determine their starting point
AI Mentor Explanation
HEAD is like the umpire's marker showing exactly which ball of the innings is currently being played, normally following the over count automatically as play proceeds. If someone manually replays an old ball for review outside the live over sequence, that marker becomes detached from the running scoreboard until play resumes normally.
Step-by-Step Explanation
Step 1
HEAD normally points to a branch
`.git/HEAD` typically contains `ref: refs/heads/main`, a symbolic reference to the current branch rather than a raw commit hash.
Step 2
The branch points to a commit
`refs/heads/main` itself contains the SHA-1 of the branch's tip commit, so HEAD indirectly resolves to that commit.
Step 3
Committing moves both forward
A new commit is created, `main` is updated to point to it, and HEAD automatically reflects the change since it just follows `main`.
Step 4
Checking out a commit detaches HEAD
`git checkout <hash>` (or a tag) makes HEAD store that commit hash directly instead of a branch reference — a 'detached HEAD' state.
Step 5
Reference history relatively
`HEAD~1`, `HEAD~2`, and `HEAD^` let you address earlier commits relative to HEAD without typing out full hashes, used heavily in reset, log, and diff.
What Interviewer Expects
- Explains HEAD as a pointer to the current branch, which points to a commit
- Knows HEAD is stored in `.git/HEAD` as a symbolic reference
- Can explain detached HEAD state and its risk of losing commits
- Understands relative references like `HEAD~1` and `HEAD^`
- Can describe what happens to HEAD during a commit versus a checkout
Common Mistakes
- Thinking HEAD is the same thing as the `main` branch specifically
- Not understanding detached HEAD, then losing commits made in that state
- Confusing `HEAD~1` (first parent, N generations back) with `HEAD^2` (second parent of a merge)
- Assuming HEAD always contains a commit hash rather than possibly a branch reference
- Forgetting to create a branch before leaving a useful detached-HEAD commit
Best Answer (HR Friendly)
“HEAD is Git's way of tracking exactly where you currently are in a project's history — essentially a 'you are here' marker. Normally it follows whichever branch you're working on, but it can also point to a specific past point in history when you're just looking around.”
Code Example
$ cat .git/HEAD
ref: refs/heads/main
$ git rev-parse HEAD
7a1c3de9f0b2c4d6e8f0a1b2c3d4e5f6a7b8c9d0
$ git checkout 7a1c3de
Note: switching to '7a1c3de'.
You are in 'detached HEAD' state...
$ git log --oneline -1 HEAD~2
5c4b3a2 Update dependencies
$ git switch main
Switched to branch 'main'Follow-up Questions
- What is a detached HEAD state and why can it be risky?
- What is the difference between `HEAD~2` and `HEAD^2`?
- How does `git switch` versus `git checkout` affect HEAD?
- What happens to HEAD during a rebase?
- How would you recover a commit made while in detached HEAD after switching away?
MCQ Practice
1. In the normal case, what does HEAD point to?
HEAD is usually a symbolic reference to the current branch (e.g. `refs/heads/main`), and that branch points to the actual tip commit.
2. What is a 'detached HEAD' state?
Detached HEAD occurs when you check out a specific commit or tag directly, so HEAD stores that commit's hash rather than following a branch.
3. What does `HEAD~1` refer to?
`HEAD~1` is shorthand for the commit one step back in history from HEAD, following the first parent at each generation.
Flash Cards
What does HEAD normally point to? — The current branch, which itself points to that branch's tip commit.
Where is HEAD stored? — In the file `.git/HEAD`, usually as a symbolic reference like `ref: refs/heads/main`.
What is detached HEAD? — A state where HEAD points directly to a commit hash instead of a branch, making new commits easy to lose if you switch away.
What does `HEAD~1` mean? — The commit one generation before the current HEAD commit.