Home › Git

You are in 'detached HEAD' state. — how to fix

Note: switching to 'a1b2c3d'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

Quick fix: To keep work you did here: git switch -c my-branch. To go back: git switch main.

What it means

Normally HEAD points to a branch (like main), and new commits move that branch forward. You checked out a commit, tag, or remote branch directly, so HEAD points to a bare commit. New commits here don't belong to any branch.

It's not an error. It's a warning that commits made now are easy to lose.

Just looking around? Go back

git switch main          # or: git checkout main

Made commits you want to keep?

Create a branch where you are, before switching away:

git switch -c my-fix

Already switched away and lost the commits?

They still exist for a while. Find them in the reflog:

git reflog
# a1b2c3d HEAD@{2}: commit: my lost work
git branch recovered a1b2c3d

Why it happens

Last updated 2026-10-27.