Detached HEAD, and how work seems to vanish
A branch is just a sticky note with a commit ID on it. Detached HEAD means you took the note off. You are standing on the commit itself, with nothing holding your place.
This is the single most common way engineers "lose" a commit, and it is also completely recoverable once you can picture what actually happened.
Committing on a detached HEAD, and getting it back
1 / 5Normally HEAD points at a branch, and the branch points at a commit. Two hops.
Where you meet this in real life
Almost nobody types a raw SHA on purpose. Detached HEAD usually arrives sideways:
- Debugging a bad release.
git checkout v2.3.1orgit checkout <sha>to see what production is actually running. Tags detach exactly like SHAs do. git bisect. Bisect works by checking out commits, so every step of a bisect run leaves you detached. That is normal and expected.- CI checkouts. Most CI systems clone and then check out the exact commit
SHA that triggered the build, so your pipeline is nearly always on a detached
HEAD. It is also why
git branch --show-currentis empty in CI and scripts that rely on it silently misbehave.
Getting out safely
If you have committed anything while detached, give it a name before you switch away:
git switch -c my-fix # creates a branch here, keeping your commitsIf you already switched away and the commits look gone:
git reflog # find the SHA: it lists everywhere HEAD has been
git branch rescue <sha> # point a branch at itThe reflog is local, per-clone, and expires unreachable entries like this one
after 30 days by default (gc.reflogExpireUnreachable; still-reachable
entries get 90 days via gc.reflogExpire). That window is why "I lost my
commit" is almost always wrong: unreachable is not deleted, and garbage
collection does not touch anything the reflog still references.
Remember this
- 1HEAD → branch → commit is normal. HEAD → commit directly is detached. That is the whole concept.
- 2Commits made while detached are real and safe: they are just unreachable, so switching branches makes them look deleted.
- 3git reflog is the undo history for HEAD itself. It is the first command to run whenever a commit seems to have disappeared.