detached HEAD state — What it means in Git
This is not really an error but a warning that confuses many beginners. HEAD points to a commit instead of a branch. Here is what that means and how to handle it.
What this warning means
In Git, HEAD is a pointer that tells Git which commit you are currently working on. Normally, HEAD points to a branch (like main), and the branch points to a commit.
When you are in detached HEAD state, HEAD points directly to a commit instead of a branch. You can still browse files, make changes, and create commits — but those commits don't belong to any branch.
The danger is that if you switch to a branch without saving your work, those orphaned commits can eventually be garbage-collected and lost. The fix is simple: create a branch before switching away.
Why it happens
Checking out a specific commit
Running git checkout a1b2c3d moves HEAD directly to that commit. Since you are not on a branch, Git enters detached HEAD state. This is the most common trigger.
Checking out a tag
Tags point to a specific commit, not a branch. Running git checkout v1.0.0 puts you in detached HEAD just like checking out a commit hash. To work from a tag, create a branch immediately.
During an interactive rebase
When you run git rebase -i, Git replays your commits one at a time in detached HEAD state. This is normal and temporary — HEAD reattaches to your branch once the rebase completes.
How to fix it
Two options: save your work to a new branch, or go back to an existing one.
1. Save work to a new branch
2. Return to a branch
3. Work from a tag
How to prevent it
Use git switch instead of git checkout
git switch is designed for branch navigation and refuses to detach HEAD unless you explicitly use --detach. This prevents accidental detached HEAD situations.
Create a branch when inspecting old commits
Instead of git checkout a1b2c3d, use git switch -c inspect-commit a1b2c3d. This gives you a branch right away, so any commits you make are safe.
Know when detached HEAD is normal
During git rebase -i or git bisect, detached HEAD is expected. Don't panic — just let the operation finish or abort it cleanly if something goes wrong.
GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.
FAQ — detached HEAD state
Master Git branches with GitQuest
Practice branching, merging, and navigating commits through interactive investigations.
Start learning Git