How to revert numerous commits

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the git category.

Last Updated: 2024-03-02

How to revert numerous commits

A revert is a reverse patch. It totally undoes a change set - but without rewriting history. Therefore it shines when you've already pushed to a remote.

To revert the last three commits, you can feed it a range:

git revert HEAD~3..HEAD

You can do the same range trick with commit hashes

git revert ads89ads..zsdf4321

If you are considering undoing multiple commits, you may want the --no-commit option so as to reduce noise from having too many revert commits:

git revert --no-commit hash1 hash2 

# ... and after this just commit every single revert as one commit 
git commit -m "Message"