Git Detached HEAD: What It Means and How to Fix It

A detached HEAD means Git has checked out a specific commit instead of a branch name. You can inspect files and even make commits in this state. If you made commits you want to keep, create a branch before switching away.

Practice Git workflows →

What does detached HEAD mean in Git?

Normally, HEAD points to your current branch, and that branch points to its latest commit. In a detached state, HEAD points directly to a commit instead. This often happens after checking out a commit hash or tag to inspect an older version.

Commits made in this state are not attached to a branch. If you switch away without saving them, they can become difficult to find later. Git may eventually remove unreachable commits during maintenance, so create a branch to keep work you care about.

Check whether HEAD is detached

Check your status and current branch:

git status
git branch --show-current

In a detached state, git status reports that you are not currently on a branch, and git branch --show-current prints no branch name.

Keep commits made in detached HEAD

If you created commits you want to keep, create a branch at your current commit before checking out another branch:

git switch -c save-detached-work

Replace save-detached-work with a descriptive branch name. The branch now points to your current commit, keeping it reachable as you continue working.

Return to an existing branch

If you did not make commits you need to preserve, switch back to the intended branch:

git switch main

Use your actual branch name if it is not main. If Git warns that local changes would be overwritten, do not force the switch. Commit, stash, or otherwise preserve those changes first.

Find a commit after switching away

If you already left detached HEAD and cannot see your commit on a branch, inspect the recent reference log:

git reflog

Find the commit in the output, then create a branch pointing to its hash:

git switch -c recover-work <commit-hash>

Replace <commit-hash> with the actual hash from your reflog. Reflog entries expire, so use this soon after noticing the lost commit.

Detached HEAD FAQ

Is detached HEAD a Git error?

No. It is a valid state often used to inspect an exact commit or tag. It only needs attention if you make commits that should be kept.

Will git switch main delete my detached changes?

Committed detached work is not automatically deleted, but it will no longer be referenced by a branch and can become hard to find. Uncommitted changes may prevent the switch or be carried along. Save important commits on a branch and check git status before switching.

Learn Git Faster by Practicing

Reading is just the first step. Real learning happens when you practice.

Start Learning Git →