Q: You're in "detached HEAD" state. What does that mean, and how do you get out of it without losing work?
Normally HEAD points to a branch (e.g. main), and the branch points to a commit. "Detached HEAD" means HEAD points directly at a commit w...
#Git #Undo, Recovery & History Rewriting #L1 #Version Control #Collaboration #Terraform State
🎙️ Candidate Opening & Architectural Context
""During a major release branch cut, we encountered this exact scenario and used Git internals to recover cleanly. The interviewer is testing: Mental model of HEAD, branches, and commits.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third.""
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1️⃣
Initial Diagnostics & Root Cause Analysis
Normally HEAD points to a branch (e.g. main), and the branch points to a commit. "Detached HEAD" means HEAD points directly at a commit with no branch in between — usually because you ran git checkout or git checkout v1.2.0 (a tag).
- No new commits made yet? Just
git switch main(or whichever branch). No data at risk. - Made commits you want to keep? Create a branch from where you are *before* switching:
- Already switched away and panicking?
git reflogshows everywhereHEADhas been; find your commit hash andgit branch rescue-branch.
2️⃣
Remediation & Permanent Safeguards
It's not broken, just risky: any new commits you make in this state aren't on a branch. If you switch away, those commits become unreachable and will eventually be garbage-collected. To recover: Now those commits are anchored to a branch and safe.
git switch -c rescue-branch
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: No new commits made yet? Just git switch main (or whichever branch). No data at risk.."
⚡ 60-Second Elevator Pitch Talking Points
- No new commits made yet? Just git switch main (or whichever branch). No data at risk.
- Made commits you want to keep? Create a branch from where you are *before* switching:
- Already switched away and panicking? git reflog shows everywhere HEAD has been; find your commit ...
Advertisement