Black Friday sale: 30% off Pro and Unlimited, until 22 December

How to undo the last commit in Git before you push it

Take the newest commit back out of your history and keep your work, as staged changes or plain edits, as long as it has not been pushed.

You committed, looked at the result, and it is wrong: a file that should not be in there, a message you regret, a change that belongs on another branch. Nobody has seen the commit yet, so you can take it back. The rule that keeps this safe: undo only commits that are not pushed.

Check that it is still yours alone

Refresh your copy of the remote, with pruning off:

git fetch --no-prune --no-prune-tags

Then list what your branch has that its upstream doesn’t.

git log --oneline @{u}..HEAD
1694cb6 Add coupon field
236d4d7 Fix cart rounding

The newest commit is at the top. If it is on this list, it was never pushed and you can rewrite it. If the list is empty, skip to what to do once it is pushed.

Undo it and keep the changes staged

This is the usual choice: the commit goes away, and everything in it waits, staged, ready to commit again.

git reset --soft HEAD~1

HEAD~1 means one commit before where you are now. Check the result:

git status --short
A  coupon.js
M  notes.md

Both files are exactly as you committed them, still staged. Change what you need and commit again.

Undo it and keep the changes as plain edits

Want to choose again what goes in? Drop the staging as well:

git reset HEAD~1
Unstaged changes after reset:
M	notes.md
git status --short
 M notes.md
?? coupon.js

The work is all still in your folder. Nothing is staged, and a file the commit added shows up as untracked. Stage only the parts you want, then commit.

Either way, look at the list from the first step again. The commit is gone from it:

git log --oneline @{u}..HEAD
236d4d7 Fix cart rounding

Only the message or one forgotten file

If the commit is right but its message or content is a little off, you don’t need to undo it. Amend it:

git commit --amend -m "Add coupon input"

Stage a forgotten file first and it joins the commit. Amend swaps the commit for a new one with a new ID, which is why it is safe only before the push.

The one that loses work

git reset --hard HEAD~1

This also resets your files, so the commit’s changes vanish from your folder, not just from the history. Use it only when you really want the work gone.

Did it by accident? The commit is still in Git’s reflog for a while:

git reflog -3
236d4d7 HEAD@{0}: reset: moving to HEAD~1
1694cb6 HEAD@{1}: commit: Add coupon field
236d4d7 HEAD@{2}: commit: Fix cart rounding

Go back to the line before the reset:

git reset --hard HEAD@{1}
HEAD is now at 1694cb6 Add coupon field

That brings back committed work only. Edits that were never committed are gone for good after a hard reset.

Once it is pushed

Other people may have pulled that commit. See how to revert a pushed commit to undo the change while keeping the shared history.

You can rewrite a pushed commit when the branch is yours alone, such as a feature branch nobody else uses. Undo it as above, then push with a lease so you never overwrite work you haven’t seen. See how to force push with a lease.

Before you push, look

The best undo is the one you don’t need. Read how to see what you are about to push first, and how to find unpushed commits across every repository you have.

Or see it before you click

push& (Pushand) is a Mac menu bar app that lists the commits waiting on each branch, with their files, before you push. Spotting the wrong commit while it is still local is the cheap moment to fix it. Free for 3 repositories, and every install starts with a 14-day trial, no card and no account.

See how it works

More Git how-tos