More Git answers

by Danny
Published 2 min read

Detached HEAD means you are on a commit, not a branch. Learn how you got there, why new commits can go missing, and how to keep them safely with git switch -c.

The short answer

#

You have checked out a specific commit instead of a branch. Looking around is harmless. If you have not made any commits, switch back to a branch. If you have made commits you want to keep, create a branch where you are first.


_10
# no new commits: go back to your branch
_10
git switch main
_10
_10
# made commits you want to keep: give them a branch
_10
git switch -c my-fix

Why it happens

#

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 detached HEAD state, HEAD points straight at a commit, with no branch in between. Git shows a message like this:


_10
Note: switching to 'a81b4de'.
_10
_10
You are in 'detached HEAD' state. You can look around, make experimental
_10
changes and commit them, and you can discard any commits you make in this
_10
state without impacting any branches by switching back to a branch.

You can still commit, but no branch records those commits. As soon as you switch away, nothing points at them, and they become hard to find.

You usually get here by checking out something that is not a local branch:

  • A commit hash, for example git checkout a81b4de.
  • A tag, for example git checkout v2.1.0.
  • A remote-tracking branch, for example git checkout origin/main.
  • git switch --detach, on purpose.
  • In the middle of a rebase or a git bisect, where Git detaches HEAD while it works.
  • Inside a submodule, which checks out a fixed commit by default.
  • In many CI systems, which check out the exact commit being built.

To confirm, run git status. It will say something like "HEAD detached at a81b4de".

How to fix it

#

Keep your commits with git switch -c

This is the most important step if you have committed anything. It creates a new branch at the current commit and attaches HEAD to it.


_10
git switch -c my-fix
_10
# older equivalent: git checkout -b my-fix

You can then merge it into another branch, or push it and open a pull request:


_10
git switch main
_10
git merge my-fix

git switch has been available since Git 2.23. On older versions, use git checkout -b.

Go back to a branch

If you were only looking around, switch back. Any uncommitted edits come with you, or Git stops and tells you to commit or stash them.


_10
git switch main
_10
# or return to wherever you were before
_10
git switch -

Rescue commits after you already left

When you switch away from commits that are not on any branch, Git warns you and prints their hash:


_10
Warning: you are leaving 2 commits behind, not connected to
_10
any of your branches:
_10
_10
a81b4de Add login form validation
_10
7c0e5b2 Add login form
_10
_10
If you want to keep them by creating a new branch, this may be a good time
_10
to do so with:
_10
_10
git branch <new-branch-name> a81b4de

Copy the hash and create the branch. If the message has scrolled away, find the commit in git reflog.


_10
git branch my-fix a81b4de

Work on a remote branch properly

If you meant to work on a branch that exists on the remote, switch to it by name rather than checking out origin/main or similar. Git creates a local branch that tracks the remote one.


_10
git fetch
_10
git switch feature/login

Finish the operation that detached you

If you are mid-rebase or mid-bisect, do not create branches or switch away. Complete or cancel the operation, and Git reattaches HEAD for you.


_10
git rebase --continue # or: git rebase --abort
_10
git bisect reset

Common pitfalls

#
  • Committing for an hour in detached HEAD, then switching branches and losing track of the work. Create a branch as soon as you decide to keep anything.
  • Trying git switch origin/main. Unlike checkout, switch refuses to detach HEAD unless you add --detach, and it reports that a branch is expected.
  • Assuming detached HEAD is an error. Checking out a tag to test an old release is a normal reason to be detached.
  • Pushing from detached HEAD with plain git push. There is no current branch to push; create a branch first, or push explicitly with git push origin HEAD:my-fix.
#

Was this resource helpful?