How to Undo the Last Git Commit
Undoing, fixing and recovering commits with reset, amend, revert and reflog.
You may encounter times when you commit and immediately wish you hadn't. Maybe you forgot a file, typed the wrong message or committed to the wrong branch. Git has a fix for all of these, but the commands aren't always easy to find when you're in a panic.
My go-to bookmark for this is Oh Shit, Git!?! by Katie Sylor-Miller. It's a list of git messes written in plain English, each with the commands to get out of it. If the swearing isn't your thing, the same guide lives at Dangit, Git!?!. A lot of what's below comes from recipes I've used from that site over and over.
If you're still getting comfortable with git, start with these everyday git commands first.
Important! Everything here except git revert rewrites history. Only use them
on commits you haven't pushed yet.
Undo the Commit but Keep Your Changes
This is the one I use most. It removes the last commit but leaves all of your changes in place, staged and ready to commit again:
git reset HEAD~ --soft
HEAD~ means "the commit before the current one". Your files don't change at
all, only the commit is gone.
Undo the Commit and Throw Away the Changes
If you want the commit and everything in it gone for good:
git reset HEAD~ --hard
Your files go back to exactly how they were before that commit. Be careful with this one, since any uncommitted work is thrown away too.
Add a Forgotten File to the Last Commit
When you commit and then notice you missed a file or a small change, you don't need a whole new commit. Stage the change and amend:
git add .git commit --amend --no-edit
--no-edit keeps the same commit message. Your last commit now includes the
change.
Fix the Last Commit Message
Leave off --no-edit and git opens your editor so you can rewrite the message:
git commit --amend
You can also pass the new message right on the command line with
git commit --amend -m "Fix the header layout".
Move a Commit to a New Branch
If you committed to master but meant to start a new branch, create the branch
first, then remove the commit from master:
git branch new-feature # the branch keeps your commitgit reset HEAD~ --hard # remove the commit from mastergit checkout new-feature # your commit is here
The new branch is created from where master was, so the commit comes along
with it.
Undo a Commit You Already Pushed
Once a commit is pushed and someone else might have pulled it, don't rewrite it.
git revert makes a new commit that undoes the old one instead:
git log # find the hash of the commit to undogit revert a1b2c3d # create a commit that reverses itgit push
The history still shows the original commit, but its changes are gone. This works for any commit, not only the last one.
The Undo Button for Your Undo
If a reset went wrong and you lost a commit, it's almost never really gone.
git reflog shows every place HEAD has been, across every branch:
git reflog# a1b2c3d HEAD@{0}: reset: moving to HEAD~# e4f5a6b HEAD@{1}: commit: Add the contact form# ...git reset HEAD@{1} --hard
Find the entry from before things broke and reset back to it. Ohshitgit calls
this git's magic time machine, and it has saved me more than once. (Leave off
--hard if you have uncommitted changes you want to keep.)
There are a lot more fixes on ohshitgit.com, like committing to the wrong branch or undoing changes to a single file. It's worth keeping open in a tab.
Have some feedback on this post? .