Skip to content

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

✗ Broken code
git checkout abc123  # detached HEAD
✓ Fixed code
git checkout -b fix-from-abc123 abc123  # new branch from commit

Checking 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 branch to see available branches

  • Check 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> then git checkout <name>

  • If you haven't committed anything, just git checkout <branch> to return

  • If 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

Related Practice

← Back to language