It reads like an error. It isn't one. Git is just telling you exactly where you are — and getting back to a branch takes one command, once you know why.
Adapted from the Git & GitHub Companion Guide.
You check out a specific commit to look at some old code, and Git prints this:
Note: switching to 'a3f92c1'.
You are in 'detached HEAD' state...
HEAD is now at a3f92c1 Fix login redirect
Nothing crashed. There's no red text, no error:, no fatal:. But "detached HEAD" sounds like something broke, so the instinct is to back out immediately and hope nothing was damaged. Nothing was.
What's actually happening
Normally, HEAD — Git's pointer to "where you currently are" — points at a branch, and that branch points at a commit. When you commit, the branch moves forward and HEAD moves with it, because HEAD is really just following the branch.
Checking out a specific commit directly (instead of a branch name) breaks that chain on purpose. HEAD now points straight at the commit, with no branch in between — "detached" from any branch. Git isn't warning you about damage. It's telling you, accurately, that if you commit right now, those commits won't belong to any branch — and will be effectively orphaned the moment you check out something else.
That's the entire risk: not corruption, just the possibility of doing new work in a spot Git won't automatically keep track of.
The fix, step by step
-
If you're just looking around — reading old code, checking what a file looked like at that commit — you don't need to do anything. Look, then check out
main(or whatever branch you were on) when you're done:
git checkout main
Nothing you did in detached HEAD state affects your branches at all.
- If you started making changes and want to keep them, don't switch branches yet — that's the one action that can strand your work. Instead, turn your current position into a real branch:
git checkout -b recovery-branch
This creates a new branch pointed at exactly where you are, commits and all, and reattaches HEAD to it. Nothing is lost.
-
If you made commits, switched away, and now can't find them, they're almost certainly still there — just unreferenced by any branch.
git reflogshows every positionHEADhas recently been at, including ones no branch points to:
git reflog
git checkout - recovery-branch <commit-hash>
Two mistakes worth knowing about ahead of time
Panicking and running something destructive. Detached HEAD state is not an error state — it's a completely normal, intentional part of how Git works, used constantly for things like checking out a tag or reviewing history. Running git reset --hard or anything else "just to be safe" is far more likely to lose work than the detached state itself ever was.
Making real changes without realizing you're still detached. This is the only version of this situation that can actually bite you — writing several commits' worth of work while detached, then checking out main without first branching off, which leaves those commits reachable only through git reflog (and reflog entries eventually expire). If you're not sure whether you're attached to a branch, git status tells you immediately — it says HEAD detached at <commit> right at the top when you're in this state, and names your branch when you're not.
A habit that prevents the scary version entirely
Before doing any real work after a git checkout <commit-hash>, get in the habit of checking git status first. If it says detached, branch off with git checkout -b before writing a single line — that one habit turns "I might have lost my commits" into "I never could have lost my commits" every time.
If you'd like more posts like this sent straight to your inbox, subscribe to the newsletter.
Prefer to dig in yourself? The Git & GitHub repo on GitHub has more free examples and exercises.
Top comments (0)