GIT revert multiple commits

      1 Comment on GIT revert multiple commits

Hello Folks, There are situations when we want to revert all the commits of a feature instead of checking out an earlier commit, because you want to retain the changes made for other features.


It was a real scenario where my project QA reported a bug which might have introduced by my changes. But I was doubtful of that. So what I did is I reverted all the commits of the feature I was working on and then tested the scenario. I had almost 20 commits for that feature and that too in the middle of others commits. So, If I had to do it manually then I would have given up. But I tried to automate it and I tried this.

Git revert multiple commits

and wallah, I was able to revert all the commits of that feature and found that the bug was not introduced by my commits :).

Video tutorial

How it works?

First you log all the commits in single line using git log –pretty=oneline then we pass the output to the grep ‘feature_name’ which filters commits of your feature. Now you want just the SHA of each commit for which we pass the output of grep command to cut -d ‘ ‘ -f1 which splits the commits message which space and gives back the first column of it which is hash from each commit.

Now that we have all the commits, we want to revert them one by one. I used xargs for this purpose. xargs takes standard output from the previous command and executes a command on each of the output. which means xargs -n1 git revert –no-edit will take each of the SHA and execute git revert on it.

And believe me it saves a lot of time :).

About Ajit Singh

Hi, I am Ajit Singh, author of I work on a lot of different technologies and tools. I started my career as a software engineer in Chennai (India) and recently I moved to Gurgaon near to my home town. I like reading and writing about technology.

One thought on “GIT revert multiple commits

  1. Jaideep Rana

    Nice one Ajit.

    This is the first post I am reading on your blog and I really liked it. Simple and crisp. Keep it up man 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *