Git: HEAD detached at commit

Fix the Git detached HEAD state caused by checking out a specific commit instead of a branch.

Git: HEAD detached at commit

Git enters a "detached HEAD" state when a specific commit is checked out instead of a branch.

When Git Produces This Error

Git displays You are in 'detached HEAD' state after checking out a specific commit hash, a tag, or a remote branch without creating a local branch.

What Causes Detached HEAD in Git

Running git checkout abc1234 (a commit hash) or git checkout v1.0 (a tag) moves HEAD to that commit without attaching it to a branch. Commits made in detached HEAD state are not on any branch and can be lost.

How to Fix Detached HEAD in Git

  1. Create a new branch from the current position to save any commits:

    git checkout -b my-new-branch
  2. Or return to an existing branch:

    git checkout main

    Any commits made in detached HEAD state that were not saved to a branch can be recovered using git reflog within 30 days.