What is the difference between HEAD, a branch, and a commit SHA in Git?
Understand the difference between HEAD, a branch, and a commit SHA in Git, how each moves or stays fixed, and what a detached HEAD state means.
Expected Interview Answer
A commit SHA is a fixed, unique ID for one exact snapshot; a branch is a movable label that points to the latest commit on a line of work; and HEAD is a pointer to whatever you currently have checked out — usually a branch, sometimes a commit directly.
The SHA is a 40-character hash that never changes and always identifies the same commit. A branch is just a lightweight, movable reference whose tip advances to a new SHA each time you commit on it. HEAD is a special reference telling Git where you are now: normally it points at a branch (so committing moves that branch), but if you check out a raw SHA, HEAD points straight at the commit and you enter a detached HEAD state where new commits belong to no branch.
- Explains how Git tracks position and history
- Clarifies why branches move but SHAs do not
- Prevents confusion in detached HEAD state
- Helps you navigate and reference commits precisely
- Foundation for reset, checkout and rebase
AI Mentor Explanation
A commit SHA is like a specific delivery permanently logged in the scorebook — ball 3.4, unchangeable forever. A branch is like the phrase 'the current over', a label that keeps sliding forward to whatever ball is bowled next. HEAD is the scorer's finger on the book showing which ball is live right now: usually resting on 'the current over', but it can point straight at an old delivery when you review it.
Step-by-Step Explanation
Step 1
Inspect the SHA
Run git log --oneline to see immutable commit SHAs; each identifies one exact snapshot.
Step 2
See the branches
Run git branch to list movable labels; each points at the tip commit of its line of work.
Step 3
Find HEAD
Run git rev-parse --abbrev-ref HEAD or cat .git/HEAD to see what HEAD currently points to.
Step 4
Commit and watch it move
Commit on a branch and note the branch tip and HEAD advance together to the new SHA, while old SHAs stay fixed.
Step 5
Detach HEAD
Run git checkout <sha> to point HEAD directly at a commit; new commits here belong to no branch until you create one.
What Interviewer Expects
- SHA is immutable and identifies one snapshot
- A branch is a movable pointer to a tip commit
- HEAD points to the current checkout, usually a branch
- Understanding of detached HEAD state
- How committing moves both HEAD and the branch
Common Mistakes
- Thinking a branch stores commits rather than pointing to one
- Believing HEAD is always a branch
- Confusing detached HEAD with a normal branch checkout
- Assuming a SHA can change or move
- Not knowing new commits in detached HEAD can be lost
Best Answer (HR Friendly)
“A commit SHA is a permanent ID for one exact version of the code. A branch is a moving label that always points to the newest version on a line of work. HEAD marks where you are right now — usually on a branch, but sometimes pointed straight at a specific version.”
Code Example
# List immutable commit SHAs
git log --oneline
# List branches (movable pointers); * marks the current one
git branch
# Show what HEAD points to right now
git rev-parse --abbrev-ref HEAD # e.g. main
cat .git/HEAD # ref: refs/heads/main# Point HEAD straight at a commit -> detached HEAD
git checkout 9f2c1a7
git rev-parse --abbrev-ref HEAD # prints HEAD (detached)
# Preserve work by making a branch from here
git switch -c experiment
# Back on a branch, committing moves both HEAD and the branch tip
git switch main
git commit -m "New work" # main and HEAD advance to a new SHAFollow-up Questions
- What is a detached HEAD state and how do you recover from it?
- How does git reset move HEAD and a branch differently?
- What is the difference between HEAD, HEAD~1 and HEAD^?
- How are lightweight refs like branches and tags stored on disk?
- Why can commits made in detached HEAD be garbage collected?
MCQ Practice
1. Which statement about a commit SHA is correct?
A commit SHA is a content-based hash that permanently identifies one commit; it never changes or moves.
2. What does HEAD normally point to?
HEAD usually points at the current branch, so committing advances that branch; checking out a raw SHA detaches HEAD onto the commit itself.
3. In a detached HEAD state, new commits...
With HEAD pointed at a commit rather than a branch, new commits are not on any branch and may be garbage collected unless you create a branch.
Flash Cards
What is a commit SHA? — An immutable 40-char hash identifying one exact commit/snapshot; it never moves.
What is a branch? — A lightweight movable pointer to the tip commit of a line of work; it advances on each commit.
What is HEAD? — A pointer to your current checkout — usually a branch, sometimes a commit directly (detached HEAD).
What is detached HEAD? — HEAD points straight at a commit, not a branch; new commits belong to no branch and can be lost.
What moves when you commit on a branch? — Both the branch tip and HEAD advance to the new SHA; older SHAs stay fixed.