Undo Git Commit Amend

Here's how to undo a git commit amend (and just about anything else).

What is an Amend

When you amend, you're adding extra content to an existing commit. Your amending (or augmenting) something additional onto something original. And it becomes a new thing.

But if I do a git log after an amend, I still have just one commit. But the commit hash has changed. And that new commit contains all our combined, new content. But that's because git log will only show you the public log (shared on clone) of your commits.

What is the Reflog

But there is a reference log, or reflog, that one can run with git reflog. This includes all commits, including private commits. Commits are actually immutable, and it's representing by a single hash. When you amend (or rebase, etc), you have something new.

After our amend, let's run a git reflog, and we'll see something like this:

b03ac022c4 HEAD@{0}: commit (amend): My updated commit message from amend
96ebddfdf2 HEAD@{1}: commit: My original commit message

We see the new (amended) commit (in index 0) and the original commit (index 1).

Separate Out Amend

So, we made a mistake. We need to undo something, right? How do we disentangle what we just amended with the original commit? Well, it's easy now that we know we have 2 commits (one that's private now). We exposed it with git reflog and can now reset to it by commit hash:

git reset --soft 96ebddfdf2

Undo Shortcut

We can do the same thing with a bit of shorthand. If the amend was the latest commit, then the one right before was the original. It's in the index 1 position, and we can reset to it with this syntax:

git reset --soft HEAD@{1}

See Your Result

Don't you want to see reflog again! It tells you everything:

get reflog

96ebddfdf2 HEAD@{0}: reset: moving to 96ebddfdf2
b03ac022c4 HEAD@{1}: commit (amend): My updated commit message from amend
96ebddfdf2 HEAD@{2}: commit: My original commit message

You're back to the original, immutable commit, 96ebddfdf2. And the reflog knows you reset.

git status, likewise, will show you that your amend commit, b03ac022c4 is back, dirty, and files still staged.

Now, enough undo -- onward, onward to victory!