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

2

11

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.

Lowly answered 19/3, 2020 at 22:45 Comment(0)
S
20

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.

Sailor answered 19/3, 2020 at 23:2 Comment(2)
Nope.... the branch is whatever branch you are working on. master? develop? anything else?Sailor
Ok. So for my application, I used master for <the-branch>, then after the final line above I did a git checkout -b feature/new-master then I could push this to the remote and create a pull request for it. That did what I wanted.Lowly
N
4

Step1

Create a new backup branch first and keep it aside. (backup-branch) Create a new branch from master or dev wherever you want to revert.(working-branch)

git revert commitid1

git revert commitid2

git revert commitid3.... is the best option.

dont do git reset --hard commitid it will mesh up your indexing.

Reverting is the safe option.

i have done 180 revert commits.

Step2

git log -180 --format=%H --no-merges use this command to print all the commit ids alone.

it will ignore the merge commit id.

commitid1 commitid2 commitid3 ..... it will print like that.

Copy it to sublime ctrl+a -> ctrl +alt + l add

git revert --no-commit commitid1

git revert --no-commit commitid2

git revert --no-commit commitid3

Copy all and paste in the command prompt. All your commits will be reverted. now do a git commit.

Then do git push. create a merge request to master.

Step3

How to verifiy?

create a new branch (verify-branch).

YOu can verify it by doing a

git reset -hard commitidX. This is the commit id to which you need to revert.

git status It will give you the number of commits behind the master.

git push -f

Now compare this branch with your working-branch by creating a pull request between them. you will see no changes means your working branch successfully reverted back to the version you're looking for.

Neckline answered 16/3, 2021 at 22:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.