What "detached HEAD" means in Git, and how to get out of it

Tested: we reproduced this error and confirmed the fix on Ubuntu 24.04 with Git 2.43.0, November 29, 2026.

You’re looking at a specific commit instead of a branch. That’s fine for reading old code, but new commits made here don’t belong to any branch. To get back, switch to your branch:

git switch main

If you made commits you want to keep, put them on a new branch first:

git switch -c my-work

We reproduced the whole sequence with Git 2.43: checked out an old commit, committed, switched away and then recovered the commit with git reflog.

What detached HEAD means

HEAD is Git’s pointer to what you have checked out. Normally it points to a branch, and the branch points to a commit, so each new commit moves the branch forward. When HEAD is detached it points straight at a commit, so new commits have no branch to move. You can see the difference in the file Git keeps it in:

cat .git/HEAD

In our test this printed ref: refs/heads/main on a branch, and a bare commit hash (6d15ec01a03f...) once HEAD was detached.

git switch --detach <commit> and git checkout --detach do this on purpose. Build tools and CI systems use them to check out one exact commit.

What Git tells you

When you check out a commit, Git prints:

Note: switching to '9c94c7986c02bf00c99f4b11e1bac851d8fa37e1'.

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.

and git status starts with HEAD detached at 9c94c79.

How you got here

What you ran Why HEAD detached
git checkout <commit-hash> You asked for a commit, not a branch
git checkout v1.2.0 Tags point at commits
git checkout origin/main A remote-tracking branch, not your local one
A rebase that stopped on a conflict Git detaches HEAD while it replays commits
Your CI or IDE checked out a commit Common in build systems

Fix: keep the work you did

Create a branch where you are, then carry on or merge it:

git switch -c my-work
git switch main
git merge my-work

You can also carry uncommitted changes across: git switch -c my-work keeps whatever you’ve edited or staged. In our test a staged file was still staged on the new branch.

To copy just one commit onto main instead of keeping a branch, cherry-pick it:

git switch main
git cherry-pick <hash>

In our test this added the commit made in detached HEAD to main as a new commit.

Fix: you already switched away

When we switched to main after committing in detached HEAD, Git warned you are leaving 1 commit behind, not connected to any of your branches and printed its hash. Use that hash, or find it in the reflog:

git reflog
git branch rescue <hash>

In our test git branch rescue HEAD@{1} brought the commit back onto a branch.

Fix: a rebase left you detached

Finish or cancel the rebase instead of switching branches:

git status              # shows "interactive rebase in progress" or similar
git rebase --continue   # after fixing conflicts and git add
git rebase --abort      # to go back to before the rebase

Confirm it worked

git branch --show-current prints a branch name instead of nothing, and git status starts with On branch.

FAQ

Will I lose commits made in detached HEAD?

Not straight away. If you switch away without saving them to a branch, they're no longer on any branch, but git reflog still lists them for weeks until Git cleans them up. Create a branch from the commit to keep it.

Is detached HEAD bad?

No. It's the normal state when you check out a tag, an old commit or when a rebase stops partway. It only matters if you commit there and then forget to save the work to a branch.

How do I get back to main?

git switch main (or git checkout main on older Git).