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_10git switch main_10_10# made commits you want to keep: give them a branch_10git 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:
_10Note: switching to 'a81b4de'._10_10You are in 'detached HEAD' state. You can look around, make experimental_10changes and commit them, and you can discard any commits you make in this_10state 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 detachesHEADwhile 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.
_10git 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:
_10git switch main_10git 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.
_10git switch main_10# or return to wherever you were before_10git 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:
_10Warning: you are leaving 2 commits behind, not connected to_10any of your branches:_10_10 a81b4de Add login form validation_10 7c0e5b2 Add login form_10_10If you want to keep them by creating a new branch, this may be a good time_10to 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.
_10git 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.
_10git fetch_10git 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.
_10git rebase --continue # or: git rebase --abort_10git 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. Unlikecheckout,switchrefuses to detachHEADunless 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 withgit push origin HEAD:my-fix.
Related reading
#Was this resource helpful?