Lesson Completion
Back to course

Rescue Detached HEAD Work

Beginner
7 minutes4.7Git

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:

TriggerCommandWhy
Checkout a commitgit checkout abc1234HEAD → commit, not a branch
Checkout a taggit checkout v1.0Tags aren't branches
During rebasegit rebaseTemporarily detaches HEAD
After bisectgit bisectJumps 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

flowchart TD DETACHED["😰 Detached HEAD<br/>Commits made here"] --> SAVE{"Want to keep?"} SAVE -->|"Yes"| BRANCH["git switch -c rescue<br/>Commits are safe!"] SAVE -->|"No"| SWITCH["git switch main<br/>Commits will be lost"] style DETACHED fill:#1a1a2e,stroke:#ffd700,color:#ffd700 style BRANCH fill:#1b2d1b,stroke:#53d8fb,color:#53d8fb style SWITCH fill:#2d1b1b,stroke:#e94560,color:#e94560

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

bash
# ─── 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 reflog can 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 reflog for ~30 days, after which garbage collection may remove them permanently.

Topics Covered

Git RecoveryGit Fundamentals

Tags

#git#detached-head#recovery#beginner-friendly

Last Updated

2026-02-13