Git Detached HEAD: What It Is and How to Fix It
Git dropped you into "detached HEAD" and the commits you just made seem to have vanished. Here's what happened and how to get your work back.
Key takeaways
- Detached HEAD means HEAD points at a commit instead of a branch.
- If you made no commits, run git switch main.
- If you committed, run git switch -c new-branch before leaving to keep the work.
- If you already switched away, find the commit in git reflog and run git branch recovered with that SHA.
On this page
Detached HEAD means HEAD points at a commit instead of a branch. No commits made? Run git switch main. Committed work you want to keep? Run git switch -c my-branch before you leave. Already switched away and lost commits? Run git reflog, find the SHA, then git branch recovered <sha>. Nothing is broken; it's a warning.
Verified against
Git 2.56.0, against the git-scm.com manual pages for git-checkout (DETACHED HEAD section), git-switch, git-reflog and git-gc. Checked October 2026.
What HEAD normally points at
HEAD is Git's pointer for "where you are right now." Normally it points at a branch, like main, and the branch points at a commit. When you commit, the branch moves forward and drags HEAD along. That indirection is the whole trick: your branch name follows your work automatically.
Detached HEAD is when the middle step disappears. As the git-checkout manual puts it, HEAD "refers to a specific commit, as opposed to referring to a named branch." You land here a few ways:
git checkout <sha>orgit switch --detach <sha>: jumping to a specific commitgit checkout v1.4.0: checking out a tag (tags aren't branches)git bisectandgit rebase: both walk you across commits detached while they run- Submodules: pinned to an exact commit, so a fresh submodule checkout is detached by design
- CI systems that check out a specific commit SHA
Git tells you plainly when it happens:
git checkout a1b2c3d
Note: switching to 'a1b2c3d'.
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.
Confirm where you are at any time:
git status
HEAD detached at a1b2c3d
nothing to commit, working tree clean
git branch
* (HEAD detached at a1b2c3d)
main
HEAD detached from a1b2c3d (from, not at) means you have since made commits on top of that point.
Why it's a warning, not a failure
You can read files, build, and reproduce a bug from an old commit in detached HEAD. That's usually why you went there.
The catch is committing. Commits made in detached HEAD exist, but only HEAD refers to them. Once you switch to a branch, nothing refers to them, and the manual is explicit that they "will be deleted by the routine Git garbage collection process, unless we create a reference before that happens."
Fix 1: you made no commits, just get back
Nothing to save. Return to your branch:
git switch main
Switched to branch 'main'
git checkout main works too. git switch - returns to whatever branch you were on before.
Fix 2: you made commits and want to keep them
Do not switch branches yet. Give the work a name where you stand:
git switch -c fix/old-release-bug
Switched to a new branch 'fix/old-release-bug'
git log --oneline -3
9f8e7d6 (HEAD -> fix/old-release-bug) fix null check in parser
3c2b1a0 reproduce the crash
a1b2c3d (tag: v1.4.0) release 1.4.0
Every commit you made detached is now on a real branch. Push it, open a PR, carry on. git branch fix/old-release-bug also saves the work but leaves HEAD detached; git tag works if the commit should be a permanent marker.
Our stronger advice: create the branch before you commit, not after. The instant you decide to write code in a detached checkout, run git switch -c something. It costs nothing and turns lost commits into a non-event.
Fix 3: you already switched away
Most recoverable of all. Git usually warns you on the way out:
git switch main
Warning: you are leaving 2 commits behind, not connected to
any of your branches:
9f8e7d6 fix null check in parser
3c2b1a0 reproduce the crash
If you want to keep them by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> 9f8e7d6
Switched to branch 'main'
If that scrolled past, the reflog recorded every place HEAD has been:
git reflog -5
a1b2c3d HEAD@{0}: checkout: moving from 9f8e7d6 to main
9f8e7d6 HEAD@{1}: commit: fix null check in parser
3c2b1a0 HEAD@{2}: commit: reproduce the crash
a1b2c3d HEAD@{3}: checkout: moving from main to a1b2c3d
git branch recovered 9f8e7d6
git log --oneline -3 recovered
9f8e7d6 is the tip of the lost work, and recovered now holds it. How long do you have? Per the git-gc docs, gc.reflogExpireUnreachable defaults to 30 days for reflog entries not reachable from the current tip, which is exactly what abandoned detached commits are, versus 90 days (gc.reflogExpire) for reachable ones. Once the entry expires, the next git gc can prune the commits (it prunes unreferenced objects older than two weeks by default). So act within a month, and avoid git gc --prune=now until you've recovered.
The error strings, and what each one means
| Message | Cause | Fix |
|---|---|---|
You are in 'detached HEAD' state. | You checked out a commit, tag or remote ref | Fine for reading; git switch -c name before committing |
fatal: a branch is expected, got commit 'a1b2c3d' | git switch refuses to detach without being told | git switch --detach a1b2c3d, or switch to a branch |
fatal: You are not currently on a branch. | git push or git pull with no branch to track | git switch -c name then push, or git push origin HEAD:name |
Warning: you are leaving 2 commits behind | Switching away from detached commits | Run the git branch <name> <sha> it prints |
interactive rebase in progress; onto ... with HEAD detached | A rebase stopped mid-way | Finish with git rebase --continue or git rebase --abort; don't branch off it |
The rebase case trips people up. A paused rebase is detached on purpose, and creating a branch there leaves the rebase half done. Resolve or abort it first; rebase vs merge explains what the rebase is doing underneath.
When detached HEAD is the right tool
Inspecting old state ("did this bug exist at v2.0?") is cleaner detached than on a throwaway branch. git bisect depends on it. CI checks out an exact SHA so a build is reproducible against a snapshot, not whatever main is now. Detached HEAD in a CI log is correct behavior. If a CI step needs a branch name, read it from the CI environment variables rather than git branch.
Where this breaks
Reflog recovery only works in the clone where you made the commits: reflogs are local and never pushed, so a deleted CI workspace or a re-cloned laptop has no record. It also fails if someone ran git reflog expire --expire-unreachable=now and git gc --prune=now, which some cleanup scripts do. Uncommitted changes are different: if you discarded edits you never committed, the reflog can't help, and you need the tools in undo your last Git commit and its neighbours instead.
Questions people ask
How do I get out of detached HEAD without losing changes?
Create a branch where you are with git switch -c my-branch before switching anywhere. That attaches HEAD to the new branch, and every commit made while detached comes along. Uncommitted edits stay in your working tree too. Then merge or push the branch as usual. Only switching away first puts those commits at risk.
How do I recover commits lost after a detached HEAD?
Run git reflog and look for the commit: entries made before the checkout: moving from ... to main line. Copy the newest commit's SHA and run git branch recovered <sha>. Git's default gc.reflogExpireUnreachable is 30 days, so entries for abandoned detached commits are usually still there within a month unless someone forced garbage collection.
Is detached HEAD bad?
No. It's a normal state Git uses for tag checkouts, git bisect, rebases, submodules and CI builds. Reading files, running builds and testing old commits detached is safe. The only risk is committing there and then switching away without a branch or tag, because those commits become unreferenced and are eventually garbage-collected.
The call we'd make
Treat the warning as a checklist, not noise. Only inspecting? Ignore it and git switch back when done. Any chance you'll commit? Run git switch -c name the second you land. Commits already vanished? Run git reflog and branch off the SHA; within 30 days the work is almost certainly there. For the wider set, our Git troubleshooting guide collects the errors that catch people most.
Sources
Cite this article
Kiril Urbonas (2026). Git Detached HEAD: What It Is and How to Fix It. DevOpsNess. Updated 2026-10-31. https://www.devopsness.com/blog/fix-detached-head
[Git Detached HEAD: What It Is and How to Fix It](https://www.devopsness.com/blog/fix-detached-head) (Kiril Urbonas, DevOpsNess, updated 2026-10-31)
More from DevOps
Explore more articles in this category
Sign Container Images With Cosign: Keyless GitHub Actions Workflow, SBOM and In-Cluster Checks
One workflow that builds, signs keylessly with GitHub OIDC, attaches an SBOM and SLSA provenance, plus the Kyverno policy that refuses anything unsigned. Copy it, change three names, ship it.
Fixing DNS Resolution Failures in Kubernetes
A pod can't reach a service by name, the app throws connection errors, and everyone blames the network. Usually it's DNS, and usually it's fixable.
EU CRA Article 14: What DevOps Teams Should Wire In Now
Since 11 September 2026 the 24-hour clock for actively exploited vulnerabilities is live. The owner, the decision path, the SBOM lookup, and the runbook you need this month.
You might have missed
Evergreen posts worth revisiting.