Git detached HEAD: recover lost commits fast
Your HEAD is detached when checking out a commit hash. Fix by creating a branch from it so your commits aren't orphaned.
Quick answer
git branch temp-branch
# Now your commits are safe on temp-branch
If you already made commits while detached, git log will show them but they're floating — no branch points at them. Running that command above creates a branch at the current commit, anchoring everything.
Why this happens
Detached HEAD means you're not on any branch — you're directly on a commit. Most common trigger: you run git checkout <some-hash> to look at an old state, then start making changes. Or you accidentally check out a tag instead of a branch. I had a client last month who ran git checkout v1.0 (a tag), made two hours of changes, then panicked when git status said "detached HEAD".
Git doesn't warn you loudly enough. The terminal shows "You are in 'detached HEAD' state" but people read right past it. The commits exist — they're just not connected to any branch reference. If you switch to another branch or commit without saving them, they'll eventually get garbage-collected.
Numbered fix steps
- Stop and assess. Run
git log --oneline. Do you see the commits you made? If yes, you're in good shape. If not, skip to alternative fixes below. - Create a branch at the current spot.
git branch save-my-work # or give it a real name like "fix-print-queue-bug" - Switch to that branch.
git checkout save-my-work # Now you're on a real branch. HEAD is attached. - Push if you need to share.
git push origin save-my-work - Optionally merge or rebase this branch into your main working branch later. Don't merge raw — test first.
That's it. Two commands, ten seconds. Your commits are no longer orphaned.
Alternative fixes if the main one fails
You already left the detached state
If you ran git checkout master (or anything else) before creating the branch, those commits are still in Git's reflog for 90 days by default. Run this:
git reflog
# Look for lines like "HEAD@{2}: commit: Your message"
# Copy the commit hash (first 7 chars) from the line
Once you have the hash, create a branch from it:
git branch recovered-branch abc1234
git checkout recovered-branch
I once helped a dev who'd done git checkout master then git reset --hard after a detached HEAD — he thought his work was gone. The reflog had everything.
Your reflog is empty or expired
This is rare. If you haven't run git gc or waited months, the commits might still be findable with git fsck --lost-found:
git fsck --lost-found
# Look for "dangling commit" lines
# Each one has a hash. Use git show <hash> to see content
# Then git branch from the one you want
Honestly, I've only needed git fsck once in ten years — the reflog almost always saves you.
Prevention tip
Stop using git checkout <hash> for quick lookups. Use git log -p or git show to view old commits without moving HEAD. If you really need to inspect a past state, use git worktree add to create a temporary working directory. Keeps your main repo clean.
Set an alias to warn you:
git config --global alias.inspect '!f() { git log -p $1; }; f'. Thengit inspect abc123shows the diff without detaching.
Also, make a habit of running git status before you start editing. If it says "detached HEAD", fix it right there. Two seconds saves an hour of panic.
Was this solution helpful?