Git "Detached HEAD" Explained: What It Is and How to Get Out
"You are in 'detached HEAD' state" means you've checked out a specific commit instead of a branch. It's safe to look around; commits made here are easy to lose. How you got there, how to get back, and how to save work you did while detached.
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.
It sounds alarming. It isn't. It means you're looking at a specific commit rather than a branch. Nothing is broken.
What HEAD is
HEAD is git's pointer to "where you are right now." Normally it points at a branch, like main, and the branch points at its latest commit. When you commit, the branch moves forward and HEAD moves with it.
In detached HEAD, HEAD points straight at a commit, with no branch attached. You can look around and even commit, but no branch is keeping track of those new commits.
How you got here
Any of these:
git checkout 3f9a2c1 # a commit hash
git checkout v1.2.0 # a tag
git checkout origin/main # a remote branch, not your local one
Also common:
- In the middle of a rebase (git detaches while replaying commits). Your prompt or
git statuswill say "rebase in progress." - Submodules are checked out detached by default.
- CI systems and some tools (including some AI agents) check out a specific commit.
- Clicking an old commit in a git GUI and choosing "checkout."
Just looking? Go back to your branch
If you didn't make any changes or commits you care about:
git switch main # or: git checkout main
Done. HEAD is attached again.
(If you're mid-rebase, don't do this — finish with git rebase --continue or cancel with git rebase --abort.)
Made commits while detached? Save them first
If you committed while detached and then switch away, those commits aren't on any branch. Git warns you:
Warning: you are leaving 2 commits behind, not connected to any of your branches
To keep them, create a branch where you are before switching:
git switch -c my-experiment
Now the commits live on my-experiment. You can merge it into main later, or open a pull request.
Already switched away and lost them?
They're still there for a while. Find them with the reflog — git's log of everywhere HEAD has been:
git reflog
a1b2c3d HEAD@{0}: checkout: moving from 9e8f7a6 to main
9e8f7a6 HEAD@{1}: commit: try new layout ← your lost commit
Then give it a branch:
git branch rescued-work 9e8f7a6
(How to undo almost anything in git has more reflog rescues.)
Uncommitted changes while detached?
They're just in your working folder, like on any branch. Either create a branch and commit them (git switch -c new-branch, then commit), or stash them (git stash explained) and apply them on the right branch.
When detached HEAD is useful
It's a great way to inspect an old version:
git switch --detach v1.2.0 # what did the app look like at release 1.2?
npm install && npm run dev
git switch main # back to now
Good for finding when a bug was introduced, or comparing behaviour across versions.
The summary
- Detached HEAD = you're on a commit, not a branch. It's safe.
- Just looking?
git switch main. - Made commits?
git switch -c new-branchbefore leaving. - Lost commits?
git reflog, thengit branch name <hash>.
EasySpawn keeps your repository on a persistent server, so the reflog, stashes and branches an agent leaves behind are still there next session. See how it works or join the waitlist.
Related: Git Branches Explained · How to Undo Almost Anything in Git · Git Stash Explained · Git Cherry-Pick Explained
Keep reading
"Command Not Found": The PATH Variable Explained
"command not found" or "is not recognized as an internal or external command" usually means the program is installed but your terminal doesn't know where to look. How PATH works on Mac, Linux and Windows, how to check it, and how to add a folder permanently.
Git Push Rejected: "Updates Were Rejected Because the Remote Contains Work"
git push fails with "rejected — fetch first" or "non-fast-forward" when GitHub has commits you don't. How to pull and combine them safely, when force-pushing is fine and when it destroys work, and the other rejection messages: protected branches, large files, and secrets.