Git: You Are in 'Detached HEAD' State
Git warns you're in 'detached HEAD' state and it sounds alarming. It just means you're looking at a commit directly instead of a branch. Here's what it is and how to keep any work you do there.
You check something out and Git prints a wall of warning:
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.
It reads like a crash. It isn't. Normally HEAD — Git's "you are here" pointer — sits on a branch. Detached HEAD just means it's pointing straight at a specific commit instead. Everything works; you're simply not on a branch. The only real risk is losing new commits, and that's easy to avoid.
How you got here
You checked out something that isn't a branch — a tag, a commit hash, or a remote commit:
git checkout v1.2.0 # a tag
git checkout a1b2c3d # a raw commit hash
Git happily takes you to that exact snapshot, but there's no branch label there to move forward, so it detaches HEAD and warns you.
The risk: commits with no branch
If you make commits in detached HEAD, they aren't attached to any branch. Switch away and there's nothing pointing at them, so Git can eventually garbage-collect them. That's the whole reason for the warning.
Detached HEAD is safe for looking. It's only dangerous if you commit and then walk away — those commits have no branch holding onto them.
Just looking? Step back onto a branch
If you only wanted to inspect an old version, return to your branch:
git switch - # back to the branch you were on
# or: git switch main
Made changes you want to keep? Give them a branch
If you committed work here and want it, create a branch before switching away — that plants a permanent label on your commits:
git switch -c my-fix # new branch starting at your current commit
Now your work lives on my-fix and is safe.
The checklist
- Detached HEAD = HEAD points at a commit, not a branch. Not an error.
- Just exploring?
git switch -(orgit switch main) to reattach. - Committed something you want?
git switch -c <name>before leaving. - Don't commit and wander off — unbranched commits can be lost.
Stop reading, start building
This pairs with a hands-on BytExplorer course — do it on your own machine and actually keep the skill.