SyntaxStudy
Sign Up
Git Detached HEAD State
Git Beginner 7 min read

Detached HEAD State

Normally HEAD points to a branch. Detached HEAD means HEAD points directly to a commit. This happens when you checkout a commit hash, tag, or remote branch directly. Any commits made in this state are not on any branch and may be lost when you switch away.

Example
# Checking out a specific commit causes detached HEAD:
git checkout abc1234

# Git warns you:
# HEAD is now at abc1234 Add login form
# You are in 'detached HEAD' state.

# You can look around and make experimental commits.
# To save work done in detached HEAD — create a branch:
git switch -c experimental-fix

# Now your commits are safe on 'experimental-fix'.

# To simply go back to your branch:
git switch main

# Accidental commits in detached HEAD?
# Find them in the reflog (they stick around for ~30 days):
git reflog
# abc1234 HEAD@{0}: commit: My experimental change
# def5678 HEAD@{1}: checkout: moving to abc1234

# Recover them by creating a branch at that commit:
git branch recover-work abc1234

# Check where HEAD currently points:
git log --oneline -1
git status   # includes branch info