Short answer: detached HEAD means Git has checked out a commit instead of a branch, which happens when you check out a tag, a commit hash or a remote branch such as origin/main. Nothing is broken. To keep commits you made there, create a branch on the spot with git switch -c NAME; to leave without keeping anything, run git switch main.
git status # "HEAD detached at ..." or "HEAD detached from ..."
git switch -c my-work # keep everything here on a new branch (commits and edits)
git switch main # or: go back and leave this position
git switch - # or: go back to wherever you were beforeEvery output in this article comes from Git 2.39.5 on Debian 12, run in throwaway repositories in a scratch folder.
What detached HEAD means
HEAD is Git's pointer to what you have checked out. Normally it points to a branch name, and the branch points to a commit; a new commit moves the branch forward and HEAD moves with it. In detached HEAD state, HEAD points straight at a commit. You can still look around, build, test and even commit, but no branch name follows those commits. Git shows the difference plainly:
$ git status
HEAD detached at v1.0
nothing to commit, working tree clean
$ git branch
* (HEAD detached at v1.0)
main
$ git symbolic-ref HEAD
fatal: ref HEAD is not a symbolic ref
$ git rev-parse --abbrev-ref HEAD
HEADOnce you commit on top, the status line changes from detached at to detached from: HEAD detached from v1.0 means you have moved past the tag. In scripts, git branch --show-current prints an empty line when HEAD is detached, and git symbolic-ref -q HEAD exits with status 1, which makes a clean test.
How you ended up in detached HEAD
| What you ran | Why HEAD is detached | Usual next step |
|---|---|---|
git checkout v1.0 | A tag is a fixed commit, not a branch | git switch -c fix-v1 v1.0 |
git checkout 3f2a9c1 | You asked for a commit by hash | Look, then git switch - |
git checkout origin/main | origin/main is a remote-tracking ref you do not commit to | git switch main |
git switch --detach REF | You asked for it | Nothing, it is intended |
git rebase main | A rebase works detached; git branch shows (no branch, rebasing feature-x) | git rebase --continue or --abort |
git bisect start | Bisect checks out commits; git branch shows (no branch, bisect started on main) | git bisect reset |
git submodule update | git-submodule(1): the recorded commit is checked out on a detached HEAD | Normal; switch to a branch inside the submodule only to work there |
git switch refuses to detach by accident. Given a tag, it answers fatal: a branch is expected, got tag 'v1.0' and suggests --detach. The older git checkout detaches silently apart from the advice text, which is why most people meet this state through checkout. Creating branches from tags and commits is covered in how to create a new branch in Git.
Keep commits you made while detached
If you committed in detached HEAD and want to keep the work, give it a branch name before you leave. While you are still there, one command does it, and uncommitted edits come along too:
$ git switch -c keep-me
Switched to a new branch 'keep-me'
$ git status --short
M fgit checkout -b keep-me does the same. If you already know the commit hash, you can also name it without switching: git branch keep-me HASH. A hasty name is fine, you can rename the branch later. From there, bring the work into your main line like any other branch:
git switch main
git merge keep-me # all commits of the branch
git cherry-pick HASH # or only one commitGet back to your branch
git switch main(or the name of your branch) leaves detached HEAD. If you made no commits, nothing is lost and nothing else happens.git switch -goes back to the branch or commit you were on before, the same ascd -in the shell.- If Git refuses with
Your local changes to the following files would be overwritten by checkout, commit them on a new branch withgit switch -c, rungit stash, or throw them away withgit restore .if you are sure. - Inside a rebase or bisect, do not switch away: finish with
git rebase --continue, cancel withgit rebase --abort, or end a bisect withgit bisect reset. Each of those puts you back on your branch.
Two everyday commands also tell you that you are detached. git pull stops with You are not currently on a branch., and git push stops with fatal: You are not currently on a branch. and suggests git push origin HEAD:<name-of-remote-branch>. That works, but pushing from a named branch is easier to follow later; if a push from there is rejected, see failed to push some refs.
Recover commits after you switched away
If you leave detached HEAD with commits that no branch contains, Git warns you once and prints the hash:
Warning: you are leaving 1 commit behind, not connected to
any of your branches:
bd3a798 work on detached
If you want to keep it by creating a new branch, this may be a good time
to do so with:
git branch <new-branch-name> bd3a798
Switched to branch 'main'Missed it? The commit still exists, but git log --all no longer shows it, because no branch or tag reaches it. The reflog does: it records every position HEAD has had.
$ git reflog -n 4
20a5ef2 HEAD@{0}: checkout: moving from bd3a798febc2bb7d16923f0d7ac60798108e01c2 to main
bd3a798 HEAD@{1}: commit: work on detached
d251a2e HEAD@{2}: checkout: moving from main to v1.0
20a5ef2 HEAD@{3}: commit: commit 3
$ git branch rescue bd3a798Look for the commit: lines between the moving from main to ... line and the one where you went back. git reflog | grep commit: narrows a long list. If the reflog is gone (another clone, or git reflog expire was run), ask the object store directly:
git fsck --unreachable --no-reflogs | grep commit
git show HASH # check it is the right one
git branch rescue HASHHow long lost commits stay recoverable
Not forever. Per git-config(1), git gc expires reflog entries after 90 days (gc.reflogExpire), and entries that are not reachable from the current tip, which is exactly what an abandoned detached commit is, after 30 days (gc.reflogExpireUnreachable). Once no reflog entry points to a commit, git gc may prune it after the two week grace period of gc.pruneExpire. Recover early, and put a branch on anything you want to keep.
Silence the advice, or use detached HEAD on purpose
The long Note: switching to ... text comes from the advice setting Git names at the end of it. Once you know the state, turn it off with git config --global advice.detachedHead false; the one line HEAD is now at ... stays.
Detached HEAD is also the right state for a deploy on a VPS or dedicated server. A checkout of a release tag (git fetch --tags, then git switch --detach v2.4.0) pins the site to exactly that commit, and git status shows which tag is live. Roll back by switching to the previous tag. Just do not edit files there: if a fix is made on the server, commit it on a branch with git switch -c hotfix and push that branch, or it will be lost at the next deploy. If git answers not a git repository in a deploy script, the script runs in another folder; see not a git repository.
Frequently asked
What does detached HEAD mean in Git?
HEAD normally points to a branch name, which points to a commit. In detached HEAD state it points straight at a commit, usually because you checked out a tag, a commit hash or a remote branch like origin/main. You can look around and even commit, but no branch follows those commits.
How do I get out of detached HEAD without losing work?
If you made commits or edits you want to keep, run git switch -c new-branch-name while still detached; the branch takes the commits and the uncommitted edits. If there is nothing to keep, git switch main returns you to your branch.
How do I recover commits made in detached HEAD after switching away?
Run git reflog, find the commit: lines from the time you were detached and create a branch on the hash with git branch rescue HASH. Git keeps unreachable reflog entries for 30 days by default, so recover them soon.
Is detached HEAD bad?
No. Rebase, bisect and submodules use it on purpose, and checking out a release tag on a server is a good way to pin a deploy. It only becomes a problem when you commit there and then leave without creating a branch.