Git Detached HEAD Recovery: Save Commits and Reattach to a Branch (2026)

Published February 26, 2026 · 8 min read

If Git says you are in detached HEAD, do not panic. Your work is usually recoverable in under two minutes if you branch from the current commit before switching away.

Quick safety check: Verify exactly what changed before rescue steps in Git Diff Viewer.

If you already switched branches and think commits disappeared, jump to Git Reflog Recovery Guide for a deeper recovery path.

Table of contents

  1. What detached HEAD means
  2. Quick recovery commands
  3. If you committed while detached
  4. If work seems lost after switching
  5. Common mistakes to avoid
  6. FAQ

1. What detached HEAD means

Normally, HEAD points to a branch name such as main or feature/x. In detached HEAD, HEAD points directly to a commit hash. You can still edit and commit, but those commits are not attached to a named branch unless you create one.

State HEAD points to Risk level
Normal branch checkout Branch reference Low
Detached HEAD Specific commit hash Medium if you switch away without saving commits

2. Quick recovery commands

# check where you are
git status -sb
git rev-parse --short HEAD

# if you made commits you want to keep:
git switch -c rescue/detached-head

# if you did not make commits:
git switch main
Safe default: If you are unsure, create a rescue branch first. Branch creation is cheap and prevents accidental loss.

3. If you committed while detached

Attach those commits to a branch immediately, then push it so the remote has a backup.

git switch -c rescue/detached-head
git push -u origin rescue/detached-head

After that, you can open a PR or cherry-pick commits onto your intended branch.

4. If work seems lost after switching

Detached commits often still exist in reflog even if no branch points to them.

git reflog
# find the commit hash from detached work
git switch -c rescue/recovered <commit-hash>

If you need more edge-case recovery patterns, use the full reflog recovery walkthrough.

5. Common mistakes to avoid

Warning: If you run history-rewrite commands while detached and then switch away repeatedly, commit recovery gets harder over time. Rescue first, clean up later.

6. FAQ

Is detached HEAD an error?

Not always. It is a state. It is safe for inspection or temporary tests, but risky if you commit and switch away without creating a branch.

How do I return to my previous branch?

Use git switch - to go back to the last checked-out branch.

Can I create a branch from detached HEAD after multiple commits?

Yes. One branch creation command captures the whole detached commit chain reachable from current HEAD.

What if I already closed terminal and forgot the hash?

Use git reflog. It records recent HEAD movements and usually contains the detached commits.