Skip to main content
Git Detached HEAD: What It Is and How to Fix It

Git Detached HEAD: What It Is and How to Fix It

KU
· Founder & Chief Editor
• 8 min read•Updated •

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.

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> or git switch --detach <sha>: jumping to a specific commit
  • git checkout v1.4.0: checking out a tag (tags aren't branches)
  • git bisect and git 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:

Terminal
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:

Terminal
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:

Terminal
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:

Terminal
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:

Terminal
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:

Terminal
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

MessageCauseFix
You are in 'detached HEAD' state.You checked out a commit, tag or remote refFine for reading; git switch -c name before committing
fatal: a branch is expected, got commit 'a1b2c3d'git switch refuses to detach without being toldgit 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 trackgit switch -c name then push, or git push origin HEAD:name
Warning: you are leaving 2 commits behindSwitching away from detached commitsRun the git branch <name> <sha> it prints
interactive rebase in progress; onto ... with HEAD detachedA rebase stopped mid-wayFinish 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

Explore topics:DevOps
React
Share this post
Cite this article
Plain text

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

Markdown

[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)

KU

Kiril Urbonas

Founder & Chief Editor

610 articles
View all articles by Kiril Urbonas

You might have missed

Evergreen posts worth revisiting.