The Hook (The "Byte-Sized" Intro)
"You are in 'detached HEAD' state." Most beginners panic at this message. But detached HEAD is harmless — until you make commits and then switch away without saving them. Those commits become orphans with no branch pointing to them. They're invisible, unreachable, and scheduled for garbage collection. One simple command saves them: git switch -c <branch-name>.
📖 What is Rescue Detached HEAD Work?
When HEAD points directly to a commit instead of a branch, you're in "detached HEAD" state. Any commits you make aren't attached to a branch. If you switch away, those commits become unreachable. This lesson covers how to prevent loss and recover if you forget.
Conceptual Clarity
How detached HEAD happens:
| Trigger | Command | Why |
|---|---|---|
| Checkout a commit | git checkout abc1234 | HEAD → commit, not a branch |
| Checkout a tag | git checkout v1.0 | Tags aren't branches |
| During rebase | git rebase | Temporarily detaches HEAD |
| After bisect | git bisect | Jumps between commits |
The danger zone:
Detached HEAD: HEAD → E → D → C (no branch name!)
When you switch: HEAD → main
E → D → C ← orphaned, unreachable!
Real-Life Analogy
Detached HEAD is like writing notes on a sticky note instead of in a notebook. The notes are fine while you're holding them. But if you set them down (switch branches) without filing them (creating a branch), they'll blow away.
Visual Architecture
Why It Matters
- Common situation: Detached HEAD happens during checkout, bisect, rebase, and tag inspection.
- Easy fix: Creating a branch takes one command — but you must do it before switching.
- Recovery possible: If you already switched, reflog can find the orphaned commits.
- Not scary: Understanding detached HEAD removes the fear of exploring Git history.
Code
# ─── Prevention: save before switching ───
# You're in detached HEAD and made commits:
git switch -c my-rescue-branch
# Done! Your commits are now on a named branch
# ─── Already switched away? Recover with reflog ───
git reflog
# Look for your commits:
# abc1234 HEAD@{3}: commit: Important work in detached HEAD
git branch rescued-work abc1234
git switch rescued-work
# ─── Git's warning (don't ignore it!) ───
# When you switch away from detached HEAD with commits, Git says:
# Warning: you are leaving 2 commits behind, not connected to
# any of your branches:
# abc1234 Commit message 1
# def5678 Commit message 2
# If you want to keep them, create a branch:
# git branch <new-branch-name> def5678
# ─── Safely explore a commit (detached HEAD OK) ───
git checkout abc1234 # Look around
git log --oneline -5 # Inspect history
git switch main # Go back (no commits made = safe)Key Takeaways
- Detached HEAD means HEAD points to a commit, not a branch.
- Always create a branch before switching away if you made commits.
- Git warns you when leaving orphaned commits — read the message!
- If you already switched,
git reflogcan find the abandoned commits.
Interview Prep
-
Q: What is detached HEAD state? A: It means HEAD points directly to a commit SHA instead of a branch reference. Any new commits won't be attached to a branch. If you switch away, those commits become unreachable and may be garbage collected.
-
Q: How do you prevent losing work in detached HEAD? A: Before switching to another branch, run
git switch -c <branch-name>to create a branch pointing to your current position. This attaches your commits to a named branch. -
Q: What happens to commits made in detached HEAD after you switch away? A: They become orphaned — no branch points to them. Git warns you and shows the SHAs. They're recoverable via
git reflogfor ~30 days, after which garbage collection may remove them permanently.