What is a detached HEAD state in Git and how do you recover from it?
Understand Git's detached HEAD state, why new commits become orphaned, and how to recover work using git switch, git branch and git reflog.
Expected Interview Answer
A detached HEAD means HEAD points directly at a specific commit instead of at a branch name, so any new commits you make are not attached to any branch and can be lost once you check something else out.
Normally HEAD points at a branch ref like refs/heads/main, and committing advances that branch. When you check out a raw commit, tag, or remote-tracking ref (e.g. git checkout <sha> or a tag), HEAD points at the commit itself. You can still commit, but there is no branch following those commits, so they become unreachable after you switch away. To keep the work, create a branch at your current position with git switch -c <name> (or git branch <name> <sha>) before leaving.
- Lets you inspect or build on any historical commit safely
- Ideal for temporary experiments you may discard
- Recoverable via git reflog even after switching away
- Preserved permanently by creating a branch at the commit
- Makes bisecting and testing old states possible
AI Mentor Explanation
Imagine stepping onto the pitch to bat but your name is never written into the official scorecard — every run you score is tallied on a loose scrap of paper. When the innings ends and you walk off, that scrap is thrown away and your runs vanish from the record. Committing in detached HEAD is the same: your runs count only if you copy them onto the official sheet (create a branch) before you leave the crease.
Step-by-Step Explanation
Step 1
Recognize the state
Git prints 'You are in detached HEAD state' and git status shows 'HEAD detached at <sha>' instead of a branch name.
Step 2
Check where you are
Run git log -1 or git rev-parse HEAD to note the commit you are sitting on before doing anything else.
Step 3
Save work if you committed
If you made commits, create a branch at the current position with git switch -c my-work so those commits are no longer orphaned.
Step 4
Or leave without changes
If you only looked around, git switch main (or any branch) reattaches HEAD and nothing is lost.
Step 5
Recover if you already left
Use git reflog to find the dangling commit's sha, then git branch recovered <sha> to rescue it before garbage collection.
What Interviewer Expects
- Clear explanation that HEAD points at a commit, not a branch
- Awareness that new commits become unreachable
- Knowledge of git switch -c / git branch to save work
- Familiarity with git reflog for recovery
- Understanding of why the state is useful, not just dangerous
Common Mistakes
- Thinking commits are permanently deleted rather than dangling and reflog-recoverable
- Believing detached HEAD is always an error rather than a valid inspection mode
- Forgetting to create a branch before switching away
- Confusing detached HEAD with an unrelated hard reset
- Not knowing git reflog exists as a safety net
Best Answer (HR Friendly)
“A detached HEAD in Git just means you're viewing a specific past snapshot rather than working on a named branch, so any new work isn't attached to anything and can be lost. To keep the work you simply create a new branch at that point, and if you already moved away, Git's reflog lets you find and rescue it.”
Code Example
# Checking out a commit or tag detaches HEAD
git checkout 9f2c1a4
# git status now shows: HEAD detached at 9f2c1a4
# If you made commits here, save them onto a real branch
git switch -c experiment-fix
# Already switched away and lost the commits? Recover via reflog
git reflog
# 9f2c1a4 HEAD@{2}: commit: try new parser
git branch recovered-work 9f2c1a4Follow-up Questions
- What is git reflog and how long do entries survive?
- How does git switch differ from git checkout for this case?
- When would you intentionally work in a detached HEAD state?
- What happens to dangling commits during garbage collection?
- How does checking out a tag relate to detached HEAD?
MCQ Practice
1. In a detached HEAD state, HEAD points at?
Detached HEAD means HEAD references a commit SHA directly instead of a branch ref, so commits are not tracked by any branch.
2. Which command saves commits made in a detached HEAD state?
git switch -c (or git branch) creates a branch at the current commit so the otherwise-orphaned commits become reachable.
3. You switched away and lost detached-HEAD commits. What recovers them?
git reflog records where HEAD has pointed, letting you find the dangling commit's SHA and branch it before garbage collection removes it.
Flash Cards
What is a detached HEAD? — HEAD points directly at a commit instead of a branch, so new commits belong to no branch.
How do you save work in detached HEAD? — Create a branch at the current commit with git switch -c <name> or git branch <name>.
How do you recover lost detached commits? — Use git reflog to find the SHA, then git branch <name> <sha> before garbage collection.
How do you enter detached HEAD? — Check out a commit SHA, a tag, or a remote-tracking ref instead of a local branch.