You are in 'detached HEAD' state. appears when you check out a tag or a commit
instead of a branch. It is advice, not an error: the checkout worked. If you commit there,
give the commit a branch with git switch -c <name> before you leave, or
afterwards with git branch <name> <commit>.
The fix as run: git switch -c hotfix-1.1 while still detached, or, after
switching away, git branch hotfix-1.0 'HEAD@{1}' using the commit Git named
in its warning.
The error
> git checkout v1.0
Note: switching to 'v1.0'.
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 9896602 release one
exit=0
The empty lines were printed by PowerShell 5.1 as
System.Management.Automation.RemoteException and are shown as Git wrote them.
Read the block from the bottom up. HEAD is now at 9896602 release one says
where you are, and the exit code was 0, so nothing failed. The two commands in the middle
are the ways out that Git offers: git switch -c to keep work you make here,
and git switch - to go back to the branch you came from. The last advice
line names the config setting, advice.detachedHead, that hides this text; it
was not changed in this test, and hiding it does not change the state itself.
Why it happens
v1.0 is a tag, not a branch, so after the checkout HEAD pointed
at commit 9896602 directly, with no branch name to move. A commit made in
that state was labelled [detached HEAD 3fbd02a]. Switching back to
main printed a second warning, because no branch pointed at that commit any
more:
> git switch main
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
3fbd02a hotfix on v1.0
If you want to keep it by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> 3fbd02a
Switched to branch 'main'
exit=0
The fix
> git branch hotfix-1.0 'HEAD@{1}'
exit=0
> git log --oneline -1 hotfix-1.0
3fbd02a hotfix on v1.0
> git checkout -q v1.0
> git switch -c hotfix-1.1
Switched to a new branch 'hotfix-1.1'
> git status -sb
## hotfix-1.1
Run right after the switch, git branch hotfix-1.0 'HEAD@{1}' put a branch on
3fbd02a, the commit left behind, as the log line shows. The commit id from the
warning, git branch hotfix-1.0 3fbd02a, is the other way to write it and was
not run here. The cleaner habit is the second half of the block: while still detached, run
git switch -c with a new name, and git status -sb then reports a
normal branch.
How it was reproduced
Git for Windows 2.45.2.windows.1 in Windows PowerShell 5.1: a new repository with two
commits on main and a tag v1.0 on the first, then
git checkout v1.0, one commit made there, git switch main, and
the two ways back to a branch shown above. Repository config only.
Frequently asked
- What does You are in detached HEAD state mean in git?
- You checked out a tag or a commit, not a branch. The checkout worked; commits you make there have no branch name until you create one with git switch -c.
- How do I save commits made in detached HEAD?
- While still detached, run git switch -c with a new branch name. If you already switched away, run git branch with a new name and the commit id that Git printed in its warning.
- How do I get out of detached HEAD state?
- Switch to a branch, for example git switch main. If you made commits while detached, give them a branch first, or Git warns that you are leaving them behind.
More decoded errors in the Fixes category.