Nov 26, 2022
Per Djurner

Reverting multiple Git commits

What if you've completed a feature, pushed a bunch of commits, but then you're told that the feature wasn't needed after all? That's probably a familiar situation for most developers.

Here's a simple and safe Git command that reverts multiple commits with just one commit.

git revert --no-commit HEAD~5..

This reverts the last five commits in the current branch and stages the changes for you. All you have to do after running that is to edit the commit message and make the commit.

Note that this will only work if no other commits have been made to the branch since the ones you want to revert. It also won't work if any of the commits you want to revert is a merge commit.

Home