Oscar the owl and Vershd logo

Undo Multiple Commits Safely

9th March 2020

This is part of a Git Tips series. For the complete set for beginners and intermediate Git users, see our white paper.

The Problem

Although amending a previous commit is trivially simple and only requires you to type git commit --amend, what happens when your needs are far greater? This is when git reset steps in.

You might have multiple commits to undo, keeping the existing working files, then alter some of those files, and finally make a new commit.

The Solution

In this example, the last three commits you made to fix something are looking quite dodgy, and your boss is getting antsy about how quickly you can release that bug fix. Typical.
This removes three commits before HEAD.  It’s identical to: git reset --mixed HEAD~3.
The reset command will copy the old commit message to a file called .git/ORIG_HEAD which you can use later:
git reset HEAD~3
Or use this, which will wipe out all your existing working files:
git reset --hard HEAD~3
Or use this, which leaves both the index and all your working files unchanged:
git reset --soft HEAD~3
Now edit your files as you wish (the latest commit is currently b2819de):
... Edit as you wish...
This adds, one by one, the specific working files you wish to have in the new commit:
git add where-is/my-pay-rise.txt
Or use this, which adds all of your working files (it’s identical to: git add –a):
git add .
Finally, this creates a new commit and opens a text editor with your original commit message.
Edit it as you wish, close the message, and the commit will be performed:
git commit -c ORIG_HEAD

Pushing to the Horizon

Do you want to use Git far more easily, with no commands or parameters to remember, and with a clear overview of your repository, plus the ability to preview changes? Then make your life easier, get a free download of Vershd, the effortless Git GUI.
Designed from the ground up to prevent errors, accidental deletions, and having to write all of this morning's code. Again.

Features include:
  • Rewind is simpler and easier than using Revert or Reset. It winds the clock back on commits, letting you choose whether to keep pending files or not.
  • Advanced Context Menus. Right clicking on a commit lets you merge, cherry pick, or create a branch there and then. Files have many ways to compare them to either historic or working files on your desktop.
  • See everyone's work in the clearest way possible. Branches shows branches and their commits, where you can easily pull, push, cherry pick and more. Pending shows what you can commit or stash. Files shows commits' details. Stashes helps you apply, view and delete your stashes.
Oscar the owl and Vershd logo
 © 2020 Blightysoft
Made in England
St. George's flag of EnglandThe Union Jack, the flag of the United KingdomThe European Union flagThe United Nations flag
chevron-right linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram