computer, laptop, tech, blue computer, blue laptop, blue tech, computer, laptop, tech, tech, tech, tech, tech

Git Detached HEAD: What It Means and How to Fix It

To fix a git detached HEAD, either go back to your branch with git switch main or, if you’ve made commits you want to keep, turn the current position into a new branch with git switch -c my-branch.

# keep any work you did while detached
git switch -c my-branch

# or just go back to a branch
git switch main

Replace main with whichever branch you were on. On Git versions older than 2.23, which don’t have git switch, use git checkout -b my-branch and git checkout main.

What detached HEAD actually means

HEAD is Git’s pointer to what you currently have checked out. Normally it points at a branch name, and the branch points at a commit. When you commit, the branch moves forward and HEAD moves with it.

In a detached HEAD state, HEAD points directly at a commit instead of a branch. You can look around, build, test, and even commit, but no branch name is tracking those new commits. When you switch away, they’re left without a name and are easy to lose.

Git warns you when this happens with a message starting You are in 'detached HEAD' state, and git status shows HEAD detached at <hash>.

How you end up there

  • Checking out a specific commit: git checkout a1b2c3d
  • Checking out a tag: git checkout v2.0.0
  • Checking out a remote-tracking branch directly: git checkout origin/main
  • During an interactive rebase or git bisect, which detach HEAD on purpose while they work.
  • Some CI systems and Git submodules check out commits in detached mode by default.

It isn’t an error. It’s a perfectly valid state for inspecting old code; it just isn’t where you want to do ongoing work.

Fix 1: you didn’t change anything

Just switch back to a branch:

git switch main

You can also use git switch - to return to whichever branch you were on before.

Fix 2: you made commits you want to keep

Create a branch right where you are, before switching anywhere else:

git switch -c fix-from-detached

Your commits now belong to fix-from-detached. From there you can merge it into main, open a pull request, or rebase it as normal.

Fix 3: you already switched away and lost the commits

Don’t panic. The commits still exist for a while, they’re just unreferenced. Find them with the reflog:

git reflog

Look for the entry from when you were detached and copy its hash, then create a branch pointing at it:

git branch recovered-work <hash>

Git also prints a hint with the hash when you leave a detached HEAD with orphaned commits, so check your terminal scrollback first. Unreachable commits are eventually removed by garbage collection, so recover them sooner rather than later.

Detaching on purpose

Sometimes you want a detached HEAD, for example to test an old release without risking any branch. git switch makes this explicit:

git switch --detach v2.0.0

If you find yourself doing this often to check out other versions alongside your current work, Git worktrees are usually a cleaner answer: each worktree gets its own directory and checkout.

Gotchas

  • Uncommitted changes follow you when you switch, if they don’t conflict. If Git refuses to switch, commit them on a new branch or stash them first – see git stash explained.
  • Checking out origin/main detaches HEAD. To work on a remote branch, use git switch main; for a branch that only exists on the remote, git switch feature-x creates a local tracking branch automatically if exactly one remote has that name.
  • Rebase in progress? If you’re detached because a rebase stopped on a conflict, fix the conflict and run git rebase --continue, or git rebase --abort to go back to where you started.

More quick Git answers


Leave a Reply