🔀 The Amend That Overwrote a Commit You Actually Wanted to Keep
`git commit –amend` is meant to fix the most recent commit – a typo in the message, a forgotten file – but running it at the wrong moment (after switching what’s staged, or simply out of habit) replaces that commit’s content entirely, and the version that existed a moment earlier isn’t shown anywhere in `git log` once it’s gone.
🔎 The Problem
$ git log --oneline -1 a1b2c3d Add invoice PDF export $ git add forgotten_file.cs $ git commit --amend --no-edit # Intended to just add one forgotten file to the last commit - but the # working directory also had unrelated, half-finished changes staged # from a different task, and they got folded into the same commit too. # The original "Add invoice PDF export" commit, exactly as it was # before the amend, no longer exists under that commit hash - a1b2c3d # now points at different content entirely.
✅ Fix: The Old Commit Is Still There in the Reflog
- `git reflog` lists every position HEAD has pointed to, including the commit as it existed immediately before the amend – look for the entry just above the `commit (amend):` line, which is the original commit’s hash before it was replaced.
- `git show <old-sha>` confirms it’s the right one, then `git cherry-pick <old-sha>` (or `git branch recovered-commit <old-sha>` to just point a branch at it) brings that original content back without disturbing the amended commit that’s already there now.
- If the amend hasn’t been pushed anywhere yet, this recovery window is effectively unlimited for practical purposes – the reflog keeps entries for 90 days by default, far longer than most people take to notice an amend went wrong.
⚠️ Why This Is Worth Double-Checking Before Every Amend
- `git status` right before running `–amend` shows exactly what’s staged and about to be folded into the previous commit – a five-second check that catches this class of mistake before it happens, rather than after.
- Once an amended commit has been pushed and someone else has pulled it, recovering the original locally doesn’t undo it for them – it becomes a rewritten-history conversation, which is a good reason to treat `–amend` as local-only until a commit is shared.
Amending a commit doesn’t edit history quietly in place – it replaces one commit with another, and the reflog is what remembers the one that got replaced.
