Get out of "detached HEAD" state in Git without losing commits
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.
- HEAD detached at a1b2c3d
- HEAD detached from origin/main
- Warning: you are leaving 2 commits behind, not connected to any of your branches:
- fatal: You are not currently on a branch. To push the history leading to the current (detached HEAD) state now, use git push origin HEAD:<name-of-remote-branch>
- Not currently on any branch.
HEAD points directly to a commit instead of a branch. If you have not committed anything, just go back to your branch with git switch main; if you made commits, save them in a branch first with git switch -c my-branch.
What this error means
Normally, HEAD refers to a branch (for example main), and each new commit moves that branch forward. In "detached HEAD" state, HEAD points to a specific commit: this happens when you git checkout a hash, a tag or a remote branch such as origin/main. It is not an error, it is a warning: you can look around and even commit, but no branch name holds those commits. If you switch to another branch without naming them, they become orphaned and Git’s garbage collector will remove them once the reflog retention period has passed.
Common causes
- Checkout of a commit or tag:
git checkout a1b2c3dorgit checkout v2.0.0to look at an old version. - Checkout of a remote branch:
git checkout origin/featureinstead ofgit switch feature, which would have created a local tracking branch. - Submodules:
git submodule updatealways puts each submodule on the recorded commit, so in detached HEAD. - Rebase or bisect in progress: during a
git rebasestopped by a conflict or agit bisect, Git works in detached HEAD. - CI: many systems (including
actions/checkouton a pull request) check out a specific commit, hence "fatal: You are not currently on a branch" when a script tries to push.
Step-by-step fix
-
Understand where you are
Check whether a rebase or bisect is in progress and whether you have changes or unattached commits.
git status git log --oneline -n 5 -
Nothing to keep: go back to the branch
If you were only looking around, go back to your branch.
git switch -returns to the previous branch.git switch main # or: git switch - -
Commits to keep: create a branch
Create a branch where you are: it keeps your commits. You can then merge it into
mainor open a pull request.git switch -c my-fix git switch main git merge my-fix -
Already left? Recover the commits with the reflog
If Git warned "you are leaving N commits behind", they are not lost. Find their hash in the reflog and create a branch on it.
git reflog -n 20 git branch recovered a1b2c3d -
Work on a remote branch the right way
For a branch that exists on
origin,git switchautomatically creates a local branch that tracks it, without detaching HEAD.git fetch origin git switch feature -
Rebase, submodule or CI
Rebase: finish or abort it. Submodule: switch to a branch before committing inside it. CI: explicitly push
HEADto a named branch.# rebase in progress git rebase --continue # or: git rebase --abort # inside a submodule cd path/to/submodule && git switch main # CI git push origin HEAD:my-branch
Did this fix work for you?
Confirm it in one click: it will rise for the next developers stuck on the same error.
Keep it from coming back
- Use
git switchto change branches andgit switch --detach <commit>only when you explicitly want to explore a commit;git checkoutmixes both uses. - To test an old version, create a branch right away:
git switch -c test-v2 v2.0.0. - Show the current branch in your prompt (Oh My Zsh, Starship, posh-git) to spot a detached HEAD immediately.
Frequently asked questions
Am I going to lose my work?
Not while you stay in detached state. After that, your commits remain recoverable with git reflog during the retention period (90 days by default for reachable entries, 30 days for orphaned commits).
How do I turn off this long message?
With git config --global advice.detachedHead false. The behaviour does not change, only the warning disappears.
Why are my submodules always in detached HEAD?
That is expected: the parent repository records a specific commit for each submodule, not a branch. To follow a branch, use git submodule update --remote --merge or run git switch main inside the submodule before making changes.
Useful tools for this error
More Git & GitHub errors
- Fix "failed to push some refs" (! [rejected] fetch first) in Git
- Fix "fatal: refusing to merge unrelated histories" in Git
- Fix "Permission denied (publickey)" with Git and GitHub
- Fix "Your local changes to the following files would be overwritten by merge" in Git
- Resolve "CONFLICT (content): Merge conflict" and "Automatic merge failed" in Git
- Fix "Support for password authentication was removed" on GitHub
- Fix "GH001: Large files detected" (file over 100 MB) on GitHub