Demo, all content is generated
Question

Committed on 'detached HEAD', switched to main and my commits are gone

Solved · 381 views · asked by tuan_ng · edited

Claude Code checked out an old commit to "compare behavior", then I kept working and committed twice (didn't notice). Then I did git switch main and got a warning with some hash, which I didn't read. Now git log on main doesn't show my two commits and the changes are not in my files.

Are they deleted?

What I’ve tried

git log --all (don't see them), git stash list (empty).

Comment
Don't worry, git almost never throws commits away immediately. rune_a · edited

2 answers

Marked as helpful by the asker
rune_a · edited

Not deleted. Commits on a detached HEAD are just not on any branch. reflog remembers every place HEAD has been:

git reflog

Look for your commit messages near the top, something like a1b2c3d HEAD@{2}: commit: add reminder email. Then:

git branch rescue a1b2c3d     # put a branch on the newest of the two
git switch main
git merge rescue

That warning you skipped actually printed the hash and suggested exactly git branch <name> <hash>. Worth reading next time.

Comment
Both commits back on main. Reading the warnings from now on, promise. tuan_ng · edited
Reflog keeps entries about 90 days by default, so no rush, but don't forget. cloudkat · edited
Also made Claude use worktrees for comparisons now, like cloudkat said. tuan_ng · edited
cloudkat · edited

And tell Claude Code to use git worktree add ../compare <sha> for comparing old behavior. You get the old version in a separate folder and your working copy never leaves main.

Comment