Detached HEAD State

"Detached HEAD" sounds scary, but it's a normal Git state. Learn what it means, why it happens, how to fix it, and how to recover any work you've done while detached.

What is Detached HEAD? How to Fix Create Branch
What is HEAD in Git?

In Git, HEAD is a special pointer that tells you which commit you're currently viewing or working on. Normally, HEAD points to a branch name, and that branch points to a commit. For example, when you're on the main branch, HEAD points to "main," and "main" points to the latest commit. This is called an "attached HEAD" state.

When you run commands like git checkout main or git switch main, you're attaching HEAD to that branch. When you make new commits, the branch pointer moves forward, and HEAD moves with it automatically.

Normal (Attached) State:
HEAD → main → commit A → commit B → commit C (latest)
HEAD is just a reference—it's stored in the file .git/HEAD. You can even look at it directly with cat .git/HEAD.
What is Detached HEAD State?

Detached HEAD occurs when HEAD points directly to a commit hash instead of pointing to a branch name. In this state, you're no longer on any branch. Git displays a warning message telling you that you're in detached HEAD state and that any new commits you make won't belong to any branch.

Being in detached HEAD isn't dangerous—it's a feature. It allows you to explore old commits, inspect code from the past, or test things without affecting any branches. The problem is that new commits made while detached are "unreachable" from any branch, making them easy to lose if you switch away without creating a branch.

Detached HEAD State:
HEAD → commit C (directly, no branch)
main → commit B (unchanged)
Git warns you when you enter detached HEAD. The message says: "You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches."
How Do You Enter Detached HEAD?

You enter detached HEAD whenever you check out something that isn't a local branch name. The most common ways are:

Checking out a commit hash: git checkout a1b2c3d moves HEAD directly to that commit.

Checking out a tag: git checkout v1.0 moves HEAD to the commit tagged v1.0.

Checking out a remote branch without creating a local branch: git checkout origin/main puts you in detached HEAD.

Checking out a commit relative reference: git checkout HEAD~3 moves HEAD three commits back.

# These commands cause detached HEAD:
$ git checkout a1b2c3d # Checkout specific commit
$ git checkout v2.0 # Checkout a tag
$ git checkout HEAD~2 # Checkout parent commit
$ git checkout origin/feature # Checkout remote branch
To check out a remote branch without going detached, use git switch -c mybranch origin/mybranch or git checkout -b mybranch origin/mybranch to create a local tracking branch.
Why Would You Want Detached HEAD?

Detached HEAD isn't a mistake—it's a useful tool for several scenarios. You might want to inspect the state of your code at a specific point in history. By checking out an old commit, you can browse files, run tests, or debug issues without affecting your current work.

You might also use detached HEAD to experiment temporarily. You can make test commits, see how things work, and then discard them by simply switching back to your branch—the experimental commits will be garbage-collected later.

Another common use is examining tags. When you check out a tag (like git checkout v1.0), you enter detached HEAD. This lets you see exactly what code was released at that version without creating a branch.

Detached HEAD is perfect for exploring history, testing old versions, or temporary experiments. Just remember to create a branch if you want to keep your changes.
How to Fix Detached HEAD

If you're in detached HEAD and haven't made any new commits, fixing it is simple. Just check out or switch to a branch:

# Switch back to main branch
$ git switch main

# Or checkout a branch
$ git checkout main

# Check which branch you want to return to
$ git branch
* (HEAD detached at a1b2c3d)
  main
  feature

$ git checkout main
Switched to branch 'main'

If you've made commits while detached and want to keep them, you need to create a branch at that position before switching away. Otherwise, those commits become unreachable and may eventually be garbage-collected.

# First, create a branch at your current position
$ git branch my-new-branch

# Or create and switch to it in one command
$ git switch -c my-new-branch

# Now you're on a branch with all your commits safe
Recovering Work Done in Detached HEAD

What if you already switched away from detached HEAD without creating a branch? Don't panic—your commits aren't gone forever. Git keeps them in the reflog for about 90 days.

First, find the commit hash of your lost work using git reflog. Look for entries that show commits you made while in detached HEAD. They'll be listed with their hashes. Once you have the hash, create a branch at that commit to restore your work.

# Find your lost commits in the reflog
$ git reflog
a1b2c3d HEAD@{0}: checkout: moving from main to HEAD~2
e4f5g6h HEAD@{1}: commit: Important work in detached state
i7j8k9l HEAD@{2}: commit: More work in detached state
m9n0o1p HEAD@{3}: checkout: moving from feature to main

# Create a branch at the lost commit
$ git branch recovered-work e4f5g6h

# Switch to the new branch
$ git checkout recovered-work
The reflog is your best friend for recovering lost commits. Always check it before giving up on lost work.
How to Check Your HEAD State

You can always check whether you're in detached HEAD by running git status or git branch. Both commands clearly indicate detached state.

# git status shows detached HEAD warning
$ git status
HEAD detached at a1b2c3d
nothing to commit, working tree clean

# git branch shows no current branch
$ git branch
* (HEAD detached at a1b2c3d)
  main
  feature

# git symbolic-ref HEAD shows what HEAD points to
$ git symbolic-ref HEAD
fatal: ref HEAD is not a symbolic ref

# For attached HEAD, it shows the branch name
$ git symbolic-ref HEAD
refs/heads/main
Your shell prompt may also show detached HEAD. Many terminal prompts display (detached) or the commit hash instead of a branch name.
Best Practices to Avoid Problems

Create a branch before experimenting. If you want to try something risky or explore old code, create a branch first. git checkout -b experiment keeps your work safe.

Use git switch instead of checkout. The newer git switch command is more explicit. It won't let you enter detached HEAD without warning, and it has a -c flag to create branches.

Always check your branch before committing. Run git status or git branch before making commits to ensure you're on the right branch.

Tag important commits. If you find a commit worth remembering, tag it: git tag my-important-point. Tags won't be lost.

Push branches to GitHub. The safest way to preserve work is to push it. Even if you lose your local copy, GitHub retains your branches.

The easiest way to avoid detached HEAD confusion: always create a branch before doing significant work, even if you're just exploring old code.
Frequently Asked Questions
Is detached HEAD dangerous?
No, detached HEAD is not dangerous by itself. The only risk is that commits made while detached can be lost if you switch away without creating a branch. As long as you create a branch, your work is safe.
How do I get out of detached HEAD without losing changes?
First, create a branch: git branch save-my-work. Then switch to any other branch: git checkout main. Your work is now saved in the new branch.
I made commits in detached HEAD and switched away. Can I get them back?
Yes! Use git reflog to find the commit hashes of your lost work, then create a branch at that commit: git branch recover . Your commits are restored.
Why does git checkout tag cause detached HEAD?
Tags are pointers to specific commits that never move. Unlike branches, tags aren't designed to have new commits added to them. Checking out a tag puts you in detached HEAD because you can't commit directly to a tag.
Can I commit in detached HEAD?
Yes, you can commit. The commit will be created and HEAD will move forward, but it still won't be attached to any branch. Git will warn you that you're making commits in a detached state.
What's the difference between git checkout and git switch?
git switch was introduced in Git 2.23 to be more intuitive. git switch branch moves HEAD to a branch. git switch -c new-branch creates and switches to a new branch. git checkout still works but does many things—switching branches, restoring files, and entering detached HEAD.
How long do detached HEAD commits stay in the reflog?
By default, 90 days for reachable commits and 30 days for unreachable commits. After that, git gc may remove them permanently. Create a branch if you want to keep them indefinitely.
Does GitHub show detached HEAD?
No. Detached HEAD is a local Git concept. When you push commits made in detached HEAD to GitHub, they appear as normal commits on whatever branch you push to (or as a new branch).
Previous: Recover Lost Commits Next: Actions Failures

Detached HEAD is a feature, not a bug. Use it to explore history, test old versions, or experiment safely. Just remember to create a branch when you want to keep your work.