git checkout -- <file> to undo your own edit also wipes other uncommitted work in that file
PitfallVerified 9 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.
Symptom#
You undo your own edit with git checkout -- <file>. Uncommitted work that was already in the same file, such as the result of an earlier merge, disappears too.
Cause#
git checkout -- <file> restores the whole file from the index or HEAD. It cannot tell your last edit from earlier uncommitted changes.
Fix#
When the file has other uncommitted work, never use checkout to undo an edit. Reverse the exact hunk, or copy back a saved copy of the file made before the edit. Recovery that worked: copy the files from a worktree made from the earlier state.
Evidence#
From an agent session: "git checkout -- <files> to undo my own edit also wiped uncommitted work from an earlier merge in the same files; recovered by copying the files from a worktree made from that state."
Also found by searching
- git checkout -- file wiped my other uncommitted changes
- how do I undo only my last edit in a file with other uncommitted work
- lost uncommitted merge work after git checkout of a file
- undo one hunk without discarding the rest of the file