Fix Git that says detached HEAD

We'll check the current state, switch to a branch with git checkout or git branch, and confirm you are on a branch again.

Category
Troubleshooting · Devices & software
Time
2–5 min
Last reviewed
What you'll need
  • Git installed and a repository

Step-by-step diagnostic

Step 1 of 5
Show full guide

Steps

Goal: Check the current state, switch to a branch with git checkout or git branch, and confirm you are on a branch again.

  • Run git status. If you see “HEAD detached at” followed by a commit hash, you are in detached HEAD.
  • Good: You see the detached HEAD message. Proceed to List branches.
  • Bad: You see “On branch main”—you are already on a branch. No fix needed.

List branches

Goal: See which branch to return to.

  • Run git branch. The current branch normally has an asterisk. In detached HEAD, no branch has an asterisk. Note the branch you want (e.g. main, develop).
  • Confirm you should see the list of branches.

Switch to a branch

Goal: Leave detached HEAD by checking out a branch.

  • If you made no new commits while detached, run git checkout main (or your branch name). When you switch then you leave detached HEAD.
  • Confirm you should see “Switched to branch main” (or your branch name).

Save commits to a new branch

Goal: Keep commits you made while detached by creating a branch for them.

  • If you made commits while detached and want to keep them, run git branch new-branch-name to create a branch at the current commit. Then run git checkout new-branch-name.
  • When you do this then your commits are on the new branch. Confirm you should see the new branch created and checked out.

Verify

Goal: Confirm you are on a branch, not detached HEAD.

  • Run git status. You should see “On branch main” (or your branch name), not “HEAD detached at.”
  • Run git branch—the current branch has an asterisk. Confirm you should see a branch name.

When to get help

If you are unsure whether you made commits, run git reflog to see recent HEAD movements. If you need to recover orphaned commits, a teammate or Git expert can help. Capture the output of git status and git log --oneline -5.

Verification

  • git status shows “On branch main” (or your branch name), not “HEAD detached at.”
  • git branch shows an asterisk next to your current branch.
  • If you created a new branch to save commits, that branch exists and contains your work.

Escalation ladder

Work from the device outward. Stop when the problem is fixed.

  1. Check state Run git status to confirm detached HEAD.
  2. List branches Run git branch to see which branch to return to.
  3. Switch or save Run git checkout branch if no new commits; or git branch new-branch then git checkout new-branch if you have new commits.
  4. Verify Run git status to confirm you are on a branch.
  5. Get help Use git reflog to inspect history; ask a teammate if you need to recover orphaned commits.

What to capture if you need help

Before calling support or posting for help, have these ready. It speeds everything up.

  • Output of `git status`
  • Output of `git branch`
  • Output of `git log --oneline -5` (if you made commits)

Does git status say "HEAD detached at"?

Run `git status`. Detached HEAD means you checked out a commit, not a branch.

Run `git status`. Good: you see "HEAD detached at" followed by a commit hash. Bad: you see "On branch main"—you are already on a branch.

You can change your answer later.

Did you make new commits while detached?

Run `git log --oneline -3`. Compare to the branch you want. New commits appear at the top.

Run `git log --oneline -3`. New commits: create a branch with `git branch new-branch`, then `git checkout new-branch`. No new commits: run `git checkout main` (or your branch). Good: you know whether to save commits or just switch. Bad: unsure—use `git reflog` to inspect.

You can change your answer later.

Switch to a branch

Run `git checkout main` (or your branch name). Confirm you should see "Switched to branch main." Run `git status` to verify you are on a branch.

Save commits to a new branch

Run `git branch new-branch-name` to create a branch at the current commit. Run `git checkout new-branch-name`. Your commits are now on the new branch. Confirm you should see the branch created and checked out.

Already on a branch

You are not in detached HEAD. No action needed. Run `git branch` to see the current branch (asterisk).

Reviewed by Blackbox Atlas

Frequently asked questions

What does detached HEAD mean?
Detached HEAD means you checked out a specific commit instead of a branch. Commits you make in this state are not on any branch and can be lost if you switch away without creating a branch first.
How do I get out of detached HEAD?
Run `git checkout main` (or your branch name) to switch back to a branch. If you made commits you want to keep, run `git branch new-branch` to save them, then `git checkout new-branch`.
Will I lose my work if I switch away from detached HEAD?
Only if you made new commits and did not create a branch for them. If you just looked around and made no commits, switching away is safe. If you did commit, create a branch first with `git branch branch-name`.

Rate this guide

Was this helpful?

Thanks for your feedback.

Continue to