Skip to content

Detached HEAD ​

Every developer hits this eventually: you check out a commit SHA or a tag, and Git tells you you're in "detached HEAD" state. It sounds like something broke. It didn't. Here's what it means and how to handle it.

What detached HEAD means ​

HEAD is Git's pointer to "where you are right now." Normally it points to a branch name, and the branch name points to a commit:

HEAD -> main -> a1b2c3d

When you check out a specific commit (or a tag, or any ref that isn't a branch), HEAD points directly at the commit:

HEAD -> a1b2c3d

That's detached HEAD. You are on no branch. You can still edit, stage, and commit, but nothing points to those new commits except HEAD. If you switch to a branch, those commits become unreachable (though the reflog keeps them for 30 days).

How you get there ​

bash
# Checking out a commit SHA
git checkout a1b2c3d

# Checking out a tag
git checkout v2.0.0

# Checking out a remote branch without creating a local one
git checkout origin/feature

# Using git switch with --detach
git switch --detach HEAD~3

Working in detached HEAD ​

You can make commits normally:

bash
# Edit files...
git add .
git commit -m "experimental fix"

This creates a commit that only HEAD points to. If you run git log right now, you'll see it. But no branch points there.

Getting out safely ​

If the work is worth keeping, give it a branch name:

bash
git switch -c experimental-fix

Now your commits are on experimental-fix, a real branch. Push it like normal.

Option 2: Cherry-pick onto a branch ​

If you already left detached HEAD and need to recover the commits:

bash
# Switch back to a branch
git switch main

# Find the commit SHA from the reflog
git reflog

# Cherry-pick it onto main
git cherry-pick a1b2c3d

Option 3: Tag it ​

For throwaway experiments you might want later:

bash
git tag experiment-2026-11-10
git switch main

The tag keeps the commit reachable. You can find it later by listing tags.

Option 4: Just leave ​

If the commits were throwaway, switching away is fine. Git will warn you:

Warning: you are leaving 2 commits behind, not connected to
any of your branches:
  d3e4f5g   experimental fix
  h6i7j8k   another attempt

Those commits still exist in the reflog for 30 days. After that, Git's garbage collection deletes them.

When detached HEAD is useful ​

  • Testing an old commit. Check out the tag or SHA, run tests, switch back.
  • Bisecting. git bisect works by moving HEAD through history.
  • Rebasing. During an interactive rebase, Git works in detached HEAD and only updates the branch when the rebase finishes.
  • Quick experiments. Make a few throwaway commits to test an idea, then switch away.

Recovering lost commits from detached HEAD ​

If you made commits in detached HEAD and switched away before creating a branch, the reflog is your lifeline:

bash
git reflog
# Find the SHA of the commit you made
git show <sha>
git switch -c rescued-work <sha>

The reflog is local-only. It never pushes to remotes. If you cloned fresh and never made the commits on this machine, they are gone.

Quick reference ​

CommandWhat it does
git checkout <sha>Detaches HEAD at that commit
git switch --detach <ref>Same, explicit about intent
git switch -c <name>Creates a branch at current HEAD (saves work)
git branch <name>Creates a branch at current HEAD without switching
git reflogShows where HEAD has been recently
git log --oneline --graph --allShows all refs, including dangling commits

Pitfalls ​

PitfallWhy it bites
Committing in detached HEAD without creating a branchCommits become unreachable after switching
Pushing from detached HEADgit push refuses unless you specify a refspec
Thinking "detached" means brokenIt's a normal Git state. Nothing is corrupted
Relying on reflog across clonesReflog is local. It does not push or pull

The rule is simple: if you plan to keep the work, name it with a branch or tag. If you don't, detached HEAD is a perfectly fine place to experiment.

Git Master — Git Reference for Every Skill Level