Skip to content
FR
  1. LuzineDebug
  2. Git & GitHub
  3. You are in 'detached HEAD' state. You can look a…
Git & GitHub Beginner ≈ 5 min to fix

Get out of "detached HEAD" state in Git without losing commits

Error message
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.
In short

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.

Be the first to confirm this fix

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

  1. Checkout of a commit or tag: git checkout a1b2c3d or git checkout v2.0.0 to look at an old version.
  2. Checkout of a remote branch: git checkout origin/feature instead of git switch feature, which would have created a local tracking branch.
  3. Submodules: git submodule update always puts each submodule on the recorded commit, so in detached HEAD.
  4. Rebase or bisect in progress: during a git rebase stopped by a conflict or a git bisect, Git works in detached HEAD.
  5. CI: many systems (including actions/checkout on 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

  1. Understand where you are

    Check whether a rebase or bisect is in progress and whether you have changes or unattached commits.

    bash
    git status
    git log --oneline -n 5
  2. 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.

    bash
    git switch main
    # or: git switch -
  3. Commits to keep: create a branch

    Create a branch where you are: it keeps your commits. You can then merge it into main or open a pull request.

    bash
    git switch -c my-fix
    git switch main
    git merge my-fix
  4. 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.

    bash
    git reflog -n 20
    git branch recovered a1b2c3d
  5. Work on a remote branch the right way

    For a branch that exists on origin, git switch automatically creates a local branch that tracks it, without detaching HEAD.

    bash
    git fetch origin
    git switch feature
  6. Rebase, submodule or CI

    Rebase: finish or abort it. Submodule: switch to a branch before committing inside it. CI: explicitly push HEAD to a named branch.

    bash
    # 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 switch to change branches and git switch --detach <commit> only when you explicitly want to explore a commit; git checkout mixes 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

All Git & GitHub errors →