简体   繁体   中英

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.

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM