How to fix a git detached head state issue?

How to fix a git detached head state issue?

The terminal says `## HEAD (no branch)` just as you’re trying to get back to `main`, and it’s hard to tell whether your work is safe. I like that this status describes where Git is pointing rather than saying your files have disappeared.

You can check whether your changes are still uncommitted or already saved in a commit before choosing how to return to a branch. The status message starts to make sense when you know what HEAD points to.

What detached HEAD means

In a detached HEAD state, HEAD points directly to a commit rather than a branch name, so new commits do not advance a branch until you create or switch to one.

The site’s Git commands introduction covers the basic command vocabulary used in this recovery.

A detached HEAD can point at a commit that is also the tip of a branch. The distinction is what HEAD names, not whether that commit is old or new.

Git commit graph with HEAD attached to a branch
With an attached HEAD, the branch name points to the checked-out commit and HEAD refers to that branch.

When HEAD names a commit directly, the branch does not move as you create commits. The diagram shows the reference relationship, and Git reports the state in the next command.

Git commit graph with HEAD pointing directly to a commit
In detached state, HEAD names a commit directly rather than a branch reference.
git status --short --branch
Git status reports HEAD (no branch) in a detached checkout
git status –short –branch shows that HEAD is not attached to a branch.

Git shows ## HEAD (no branch) for a detached checkout, which is a normal state rather than an error.

Check whether your work is committed

Before switching branches, check both the working tree and the recent commit history. Uncommitted edits live in the working tree or index, while a commit made in detached state is already in Git history but has no branch name pointing to it.

git status --short
 git log -1 --oneline

If the first command lists changed files, save or commit those edits before changing branches. If the working tree is clean but the latest commit is work you need, create a branch at the current HEAD before switching away.

Repository stateSafe next move
Uncommitted edits you want to keepCommit them or use Git stash before switching
Detached commit you want to keepCreate a branch at current HEAD
No work to keepSwitch to the intended existing branch

Choose whether to keep detached commits

Use git switch -c to keep the current commit on a new branch, or switch to an existing branch if those detached commits are not needed.

1. Keep the commit by creating a branch

Run this while HEAD still points to the commit you want to save. The new branch starts at that exact commit and becomes the active branch.

Commit created while HEAD is detached
A commit made in detached state remains in history. Create a branch here before switching away to give it a durable name.
git switch -c saved-work

The branch name is your durable reference to the commit. Use a name that describes the work, then confirm HEAD is attached to it before continuing.

A new branch points to the detached commit
The new branch now names the commit that HEAD pointed to while detached.
git status --short --branch
git log -1 --oneline

Git should show saved-work in the branch line and the commit you meant to keep at the top of the log. If you need the work on another branch, switch there and merge or cherry-pick this branch’s commit. Follow the separate Git rebase workflow only when you need to replay commits onto a different base.

2. Return to an existing branch

If there are no detached commits you need, switch to the branch you intended to use. Replace main with the branch name in your repository.

git switch main

This moves HEAD to the existing branch, and new commits will advance that branch. Any detached commits you left behind may remain findable through the reflog for a time, but do not rely on that as a way to keep work.

HEAD returned to an existing branch
Switching to an existing branch attaches HEAD to that branch’s current commit.

Find a commit after leaving detached HEAD

If you already switched away and cannot see the detached commit in the current branch history, inspect the reflog. It records recent movements of HEAD, including commits and checkouts.

git reflog --oneline

Find the commit message or identifier from your detached work, then create a branch at that commit. Replace the placeholder with the identifier shown by your reflog.

Reflog resultNext action
Recent entry has the detached commit messageCreate a branch at its commit identifier
The commit is not in the visible recent entriesInspect more reflog history or another clone before cleanup
git switch -c saved-work <commit-id>

The reflog is local to your repository and entries expire according to Git’s maintenance settings. A shallow clone may not contain older commits to recover because its history is intentionally limited. If the commit is not listed, check other clones or backups before running cleanup commands.

The recovered branch commit is merged into the target branch
After naming the recovered commit with a branch, merge it into the intended branch when that is where the work belongs.

Keep the branch that names the work

A detached HEAD is safe for inspecting a commit, but a branch is the reference that keeps new work simple to find and continue. Name the commit before moving on when you need to keep it.

git switch -c saved-work

Frequently asked questions

How do I fix a detached HEAD in Git?

Switch to an existing branch if you do not need the detached commits. To keep the current commit, run git switch -c saved-work before switching away.

How do I push a detached HEAD to a branch?

Create and switch to a branch at the detached commit with git switch -c saved-work, then push that branch with git push -u origin saved-work.

What does HEAD detached at mean?

Git reports a detached HEAD when it points directly to a commit instead of naming a branch. The working tree still reflects that commit.

Can I lose commits made in detached HEAD?

The commits remain in the repository, but no branch name keeps them reachable after you switch away. Use git reflog to locate a recent commit and create a branch at it.

If the recovered branch is destined for GitHub, set up an authenticated remote with GitHub over SSH before pushing it.