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.
What you'll need
- Git installed and a repository
Step-by-step diagnostic
Quick triage — pick your path
Quick triage — pick your path
Choose the option that matches what you see. You can jump straight to that section.
- Follow this guide Work through the full procedure from checking state to verifying.
- Switch to a branch You have no new commits and want to return to main or another branch.
- Save commits to a new branch You made commits while detached and want to keep them.
- When to get help You are unsure about your commits or need to recover orphaned work.
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-nameto create a branch at the current commit. Then rungit 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 statusshows “On branch main” (or your branch name), not “HEAD detached at.”git branchshows 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.
- Check state Run git status to confirm detached HEAD.
- List branches Run git branch to see which branch to return to.
- 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.
- Verify Run git status to confirm you are on a branch.
- 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.
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.
You can change your answer later.
Switch to a branch
Save commits to a new branch
Already on a branch
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.