Home·Error messages

git · Git

The message

You are in 'detached HEAD' state.

What it means

HEAD points straight at a commit instead of at a branch name, so any commit you make here gets no name attached to it. You end up here by checking out a commit hash or a tag, by working inside a submodule, or because CI checked out one specific commit. To keep what you did, git switch -c name creates a branch right here and loses nothing. If you simply git switch main instead, the commits you made have nothing pointing at them: the reflog can still find them for a while, and then garbage collection removes them.

The fix

git switch -c rescue
Printed by
git
Git
29

Almost every git error is a refusal that means "doing that from this state would lose something"; fatal: means git stopped without changing anything, and the hint: lines under the first line usually carry the actual remedy.

Reading an error message

  • Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
  • If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
  • Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
  • The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
  • Before pasting a fix, check what it throws away. Some of these cannot be undone.

Common questions

Q. What does “You are in 'detached HEAD' state.” mean?

HEAD points straight at a commit instead of at a branch name, so any commit you make here gets no name attached to it. You end up here by checking out a commit hash or a tag, by working inside a submodule, or because CI checked out one specific commit. To keep what you did, git switch -c name creates a branch right here and loses nothing. If you simply git switch main instead, the commits you made have nothing pointing at them: the reflog can still find them for a while, and then garbage collection removes them.

Q. How do I fix it?

git switch -c rescue — before running it, check the explanation above for what this command discards.

Q. Which tool prints this?

git. It sits under Git, and the message runs to 6 words.

Errors nearby