Ffile2fix
Sign in Get started

Fix Git "You are in 'detached HEAD' state"

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 points directly at a commit instead of at a branch, which happens when you check out a commit hash, tag or remote branch. It is not an error; switch back to a branch, and if you made commits while detached, create a branch for them first so they are not lost.

Also appears as: HEAD detached at 1a2b3c4 · 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.

Common causes

  • Checking out a specific commit hash or tag, such as git checkout v2.1.0
  • Checking out a remote-tracking branch directly, such as git checkout origin/main
  • Git submodules, which are checked out at a fixed commit by default
  • An interrupted rebase or bisect that leaves HEAD on a commit
  • CI systems and some IDEs checking out a commit instead of a branch

How to fix it

  1. Check where you are. Run git status. 'HEAD detached at 1a2b3c4' confirms the state, and git log --oneline -5 shows whether you made any commits since detaching.
  2. Return to a branch if you made no changes. Run git switch main (or git checkout main). Nothing is lost if you did not commit anything while detached.
  3. Keep commits made while detached. Run git switch -c my-work to create a branch at the current commit. Then merge or rebase that branch into main as usual.
  4. Recover commits after switching away. Run git reflog, find the commit you made (for example 9f8e7d6 HEAD@{2}: commit: fix header) and run git branch rescue 9f8e7d6. Reflog entries for commits not on any branch are kept for 30 days by default, so recover them before garbage collection removes them.
  5. Finish or abort rebase and bisect. If a rebase or bisect is in progress, run git rebase --continue or git rebase --abort, or git bisect reset. These return you to your branch.
  6. Put submodules on a branch. Inside the submodule run git switch main before editing, or update with git submodule update --remote --merge so changes land on a branch.

Terminal

git status                 # HEAD detached at 1a2b3c4
git switch -c my-work      # keep commits made while detached
git switch main            # or just go back

# recover commits after leaving them
git reflog
git branch rescue 9f8e7d6

How to stop it happening again

  • Use git switch for branches and git switch --detach only when you mean to detach
  • Create a local branch from a remote one with git switch main instead of git checkout origin/main
  • Run git status before committing to confirm you are on a branch
  • Show the current branch in your shell prompt

Frequently asked questions

Is detached HEAD dangerous?

No. It is a normal state for inspecting old commits. The only risk is making commits and then switching away without creating a branch, which leaves them unreferenced.

Can I push from detached HEAD?

Not with a plain git push, because there is no current branch. Create a branch with git switch -c name first, or push explicitly with git push origin HEAD:refs/heads/name.

Why does my CI show detached HEAD?

Many CI systems check out the exact commit being built rather than a branch. This is expected; if a script needs a branch name, read it from the CI's environment variables.