What is a detached HEAD in Git?
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · git version 2.55.0.windows.5updated 2026-10-20
- tool
- Git
- versions
- Git 2.x
- os
- linux, macos, windows
Detached HEAD is the state where HEAD points directly at a commit instead of at a branch. It happens when you check out a tag, a commit hash or a remote branch such as origin/main. You can look around and even commit, but those commits belong to no branch and become hard to find once you switch away.
See it
git init -q -b main repo && cd repo
echo 1 > f && git add f && git commit -qm "v1" && git tag v1.0
echo 2 > f && git commit -qam "v2"
git switch --detach -q v1.0
git status | head -1
git symbolic-ref -q HEAD || echo "HEAD is not a branch"Output
HEAD detached at v1.0 HEAD is not a branch
Keep work done in detached HEAD
Create a branch where you are, before switching away:
git init -q -b main repo && cd repo
echo 1 > f && git add f && git commit -qm "v1" && git tag v1.0
git switch --detach -q v1.0
echo hotfix > f && git commit -qam "hotfix on v1.0"
git switch -q -c hotfix-1.0
git branch --show-current
git log --oneline -1Output
hotfix-1.0 05831c8 hotfix on v1.0
If you already switched away, git reflog shows the commit hash so you can still run git branch <name> <hash>.
Sources
Related pages
commands · gitgit reset --soft HEAD~1: undo the last commit, keep the changesUndo the last commit and keep its changes staged with git reset --soft HEAD~1. --mixed unstages them, --hard deletes them. Undo a reset with the reflog.cheat sheets · gitGit cheat sheet: everyday commandsGit cheat sheet: setup, stage and commit, branches with switch, history, undo with restore, reset and revert, stash, remotes, tags and the reflog on one page.errors · gitfatal: not a git repository (or any of the parent directories): .gitGit prints not a git repository when the current folder and none of its parents contain a .git directory. cd into the repository, run git init, or set GIT_DIR.