Detached HEAD
Error message
HEAD is now at <commit-hash>
Quick summary
You're working on a commit directly, not a branch — any commits you make won't belong to any branch and could be lost.
Why this happens
You checked out a specific commit hash (
git checkout abc123)You checked out a tag (
git checkout v1.0)A rebase or cherry-pick left you in a detached HEAD state
Minimal example
git checkout abc123 # detached HEADgit checkout -b fix-from-abc123 abc123 # new branch from commitChecking out a commit directly puts you in detached HEAD — create a branch from that commit so your new commits are saved.
How to diagnose
Run
git status— it will say "HEAD detached at <hash>"Run
git branchto see available branchesCheck if you've made commits in the detached state with
git log HEAD
How to fix
Create a new branch from the current commit:
git branch <name>thengit checkout <name>If you haven't committed anything, just
git checkout <branch>to returnIf you committed, create a branch first to save those commits
How to prevent
Always checkout branches, not commit hashes, when you want to make changes
Use
git switch -c <branch>to create and switch to a new branch
Related Resources
Related Glossary
Related Lessons
- What is Git?
Learn about HEAD and branches in Git
Related Practice
- Understanding Git Commits
Understand Git commits and HEAD.