Skip to main content

Git · 5 common causes

detached HEAD

Updated

HEAD points straight at a commit rather than at a branch, so new commits belong to no branch and are easy to lose track of.

The exact message

$ git checkout 4f2a9c1
Note: switching to '4f2a9c1'.

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 is now at 4f2a9c1 Add rate limiting

Paths, line numbers and stack frames differ on every machine, and the wording is tightened between versions. The part that identifies the problem is detached HEAD.

How to fix it

Cause 1 of 5

You checked out a commit hash, a tag, or a remote-tracking ref.

Nothing is broken — this is a normal, non-destructive state for looking at history. git switch - returns you to where you were, and git switch main goes to a named branch.

Cause 2 of 5

You already committed while detached and want to keep the work.

Name it before you leave: git switch -c keep-this-work. That converts the detached commits into an ordinary branch, and there is nothing else to do.

Cause 3 of 5

You left the state and only now realise commits are missing.

git reflog lists every commit HEAD has pointed at, orphans included. Find the hash in that list, then git branch recovered <hash> to make it reachable again. Reflog entries persist for 90 days by default, so do this before running git gc --prune.

Cause 4 of 5

A CI job checks out a specific SHA, which is detached by design.

That is expected and usually fine. If a build step needs a branch name — for a version stamp, or to push a tag — create one explicitly with git checkout -B "$CI_BRANCH" or read the branch from the CI environment variable.

Cause 5 of 5

A submodule is showing as detached.

Submodules are pinned to an exact commit, so detached is their normal state. To change one, cd into it, git switch main, commit there, then commit the updated pointer in the parent repository.

Stopping it coming back

Create a branch before you start experimenting rather than after, and configure your prompt to display the current branch so a detached HEAD is visible the moment it happens.

detached HEAD — common questions

Will I lose my commits if I switch away?
They stop being reachable by any name, which makes them invisible in normal commands, but they are not deleted immediately. The reflog can bring them back for weeks. Creating a branch before switching avoids the scare entirely.
How do I check whether I am detached?
git status says 'HEAD detached at <hash>' on the first line, and git branch --show-current prints nothing at all. Either is a reliable check inside a script.
Why does git checkout on a tag detach me?
A tag names a fixed commit, not a moving branch, so there is no branch for HEAD to follow. Committing on top of a tag needs a branch: git switch -c hotfix-1.2.1 v1.2.0.

Test the fix before you paste it back

Reproducing the failure in a scratch file is usually faster than editing the real one. AltF Code is a free in-browser editor with real syntax highlighting for HTML, CSS, JavaScript, TypeScript, Python, SQL and JSON, and it runs HTML, CSS and JavaScript live in a preview panel. No install and no sign-up.

Open the editor