Git: You are in 'detached HEAD' state
A plain-words explanation of this Git message: what it means, the usual causes, and the steps that fix it.
The messageYou are in 'detached HEAD' state.
Make the error yourself
Run with git version 2.56.0.windows.4. This is the smallest example we could find that shows it.
Setup used before these commands (a practice folder with no data)
export GIT_AUTHOR_NAME=Asha GIT_AUTHOR_EMAIL=a@example.com GIT_COMMITTER_NAME=Asha GIT_COMMITTER_EMAIL=a@example.com
git init -q -b main
echo 1 > f.txt
git add f.txt
git commit -qm first
echo 2 >> f.txt
git commit -qam secondCommands
git checkout HEAD~1Git prints (the real message)
Note: switching to 'HEAD~1'.
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.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at 70f3fa2 firstWhy it happens
This is a warning, not an error. Normally HEAD points to a branch, and a new commit moves that branch forward. When you check out a commit directly (by its hash, with HEAD~1, or a tag), HEAD points at that commit alone. You can look around and even commit, but those commits belong to no branch, and when you switch away Git may later clean them up. Many people see it by accident after clicking on an old commit in a tool.
How to fix it
- If you only wanted to look at the old version, just go back:
git switch main(orgit switch -). - If you made commits you want to keep, give them a branch before you leave:
git switch -c rescue-work. - If you lost track,
git refloglists where HEAD has been, and you can create a branch from the entry you need.
The corrected version
Commands
git checkout HEAD~1
git switch -c rescueGit prints (the corrected version)
Note: switching to 'HEAD~1'.
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.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at dff780e first
Switched to a new branch 'rescue'How to avoid it
To work on old code, create a branch from it right away with git switch -c name <commit>.
Questions
Is my work lost when I leave a detached HEAD?
Not immediately. The commits stay reachable through git reflog for a while, so you can recover them with a new branch.
This is an independent guide. It is not connected with Python, Microsoft, Google, GitHub or the Git project. Product names belong to their owners. Examples were run on 13 November 2026 in a practice folder or a blank workbook; your version may word a message slightly differently.