How to fix You are in 'detached HEAD' state. You can look around, make experimental changes and commit them in Git

GitBEGINNERLOWUpdated Dec 6, 2025ยท 213 views

The detached HEAD state occurs when HEAD points directly to a commit instead of a branch. This is not an error but a warning that any new commits you make will not belong to any branch and could be lost when you switch branches.

Quick fix

Fixes You are in 'detached HEAD' state. You can look around, make experimental changes and commit them

git status
git log --oneline -1
  1. Check your current state
  2. Return to an existing branch (if no changes to keep)
  3. Create a new branch to preserve changes
  4. Recover commits after leaving detached HEAD
git โ€” error
git status
git log --oneline -1
You are in 'detached HEAD' state. You can look around, make experimental changes and commit them

What this error means

In Git, HEAD is a pointer that normally references the current branch, which in turn points to the latest commit on that branch. When you're in a "detached HEAD" state, HEAD points directly to a specific commit rather than a branch. This state is not an error - it's a valid and sometimes useful Git state. Git shows this message to warn you that if you make commits in this state, they won't be associated with any branch. If you later checkout a different branch without saving your work, those commits become "orphaned" and may eventually be garbage collected. The detached HEAD state is actually useful for temporarily exploring historical commits, testing old versions of code, or making experimental changes you may or may not want to keep.

Typical symptoms

  • Git displays "You are in detached HEAD state" when running git status
  • The shell prompt shows a commit hash instead of a branch name
  • Running git branch shows "* (HEAD detached at <commit>)" or "* (HEAD detached from <commit>)"
  • Commits made in this state do not appear on any branch
  • Switching branches triggers a warning about potentially losing commits

Common causes

  1. Checking out a specific commit directly using git checkout <commit-hash>
  2. Checking out a tag with git checkout <tag-name>
  3. Checking out a remote branch directly without creating a local tracking branch
  4. Running git checkout --detach explicitly
  5. During an interactive rebase that encounters conflicts
  6. After a git bisect operation

How to fix "You are in 'detached HEAD' state. You can look around, make experimental changes and commit them"

1Check your current state

First, verify that you're in a detached HEAD state and see what commit you're on:

bash
git status
git log --oneline -1

Git will explicitly tell you if you're in a detached HEAD state and show you the current commit.

2Return to an existing branch (if no changes to keep)

If you haven't made any changes you want to keep, simply switch back to your main branch:

bash
git checkout main
# or
git checkout master
# or any other branch you want to return to
git switch main  # Modern alternative

This immediately reattaches HEAD to the branch.

3Create a new branch to preserve changes

If you made commits in the detached HEAD state that you want to keep, create a new branch from your current position:

bash
git checkout -b my-new-branch
# or using the modern switch command
git switch -c my-new-branch

This creates a new branch pointing to the current commit and checks it out, preserving all your work.

4Recover commits after leaving detached HEAD

If you already left the detached HEAD state and lost track of commits, you can recover them using the reflog:

bash
# View the reflog to find your commits
git reflog

# Find the commit hash you want to recover
# Then create a branch from it
git branch recovered-work <commit-hash>
git checkout recovered-work

The reflog keeps a history of all HEAD movements for about 30 days by default.

5Merge recovered changes into your main branch

Once you've saved your detached HEAD commits to a branch, you can merge them into your main development branch:

bash
git checkout main
git merge recovered-work
# Or use cherry-pick for specific commits
git cherry-pick <commit-hash>

After merging, you can delete the temporary branch if no longer needed.

Advanced notes

### When Detached HEAD is Useful

The detached HEAD state is intentionally designed for certain workflows:

- Code archaeology: Exploring historical versions of your codebase without affecting branches
- Testing old versions: Checking if a bug existed in a previous release
- Bisecting: Git bisect puts you in detached HEAD state while hunting for bugs
- Building releases: CI/CD systems often checkout specific tags in detached HEAD state

### Preventing Accidental Detached HEAD

To avoid accidentally entering detached HEAD state:

bash
# Instead of checking out a tag directly
git checkout v1.0.0  # Creates detached HEAD

# Create a branch from the tag
git checkout -b release-1.0.0 v1.0.0  # Stays on a branch

# Instead of checking out a remote branch directly
git checkout origin/feature  # Detached HEAD

# Create a local tracking branch
git checkout -b feature origin/feature  # Proper branch

### The git switch Command

Git 2.23+ introduced git switch as a safer alternative to git checkout for branch operations. It refuses to detach HEAD by default:

bash
git switch main              # Switch branches safely
git switch -c new-branch     # Create and switch
git switch --detach v1.0.0   # Explicit detach required

Using git switch makes it harder to accidentally enter detached HEAD state.

Frequently asked questions

What does "You are in 'detached HEAD' state. You can look around, make experiment" mean?
In Git, HEAD is a pointer that normally references the current branch, which in turn points to the latest commit on that branch. When you're in a "detached HEAD" state, HEAD points directly to a specific commit rather than a branch. This state is not an error - it's a valid and sometimes useful Git state.
How do I fix "You are in 'detached HEAD' state. You can look around, make experiment"?
The detached HEAD state occurs when HEAD points directly to a commit instead of a branch. This is not an error but a warning that any new commits you make will not belong to any branch and could be lost when you switch branches.
What causes "You are in 'detached HEAD' state. You can look around, make experiment"?
Checking out a specific commit directly using git checkout <commit-hash> Checking out a tag with git checkout <tag-name> Checking out a remote branch directly without creating a local tracking branch
Is the "You are in 'detached HEAD' state. You can look around, make experiment" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading