gitgithubmergepull-requestrevert

How to revert multiple pull request merges all at once and preserve history


I have a case where the Github repo master currently has a series of individual pull requests that have already been merged into master. I want to revert say the last 20 of those pull request merges but do it from the command line and preserve history.

A similar question was marked as duplicate revert multiple merges

However the answers given as already existing do not really deal with the specific question of what happens when everything you want to revert came from a series of pull requests which are all merges. Using the -m option with git revert is good for one merge at time right? So I think the answer is that there is not a good way (from the command line) to quickly revert a series of individual pull requests that have been merged and preserve history? Please correct me if I am wrong about this. If I have to do one at a time, I might as well just use the Github console and click on revert for each one.


Solution

  • If you want to have a single (later) revision where you revert the changes from all those merges, you can do it like this:

    git checkout <id-of-revision> # use the ID of the revision you would like to get your project back to (in terms of content)
    git reset --soft <the-branch> # the branch where we want to add a revision to revert all of that
    git commit -m "Reverting"
    # If you like the results
    git branch -f <the-branch> # move branch pointer to this new revision
    git checkout <the-branch>
    

    Update: Nowadays, it's simpler to do with git restore

    git checkout <the-branch>
    git restore --worktree --staged --source <id-of-revision> -- .
    git commit -m "Reverting"
    

    And you are getting to the same place as with the original recipe.