0

We recently had to revert a merge into the master branch due to a suspected bug it introduced.

Later, it turned out it wasn't a bug in my branch after all. But re-merging the same changes again turned out to be a challenge. Since git knew about the original merge, it (understandingly) stubbornly refused to merge the same changes into master again. I also didn't find a way to cleanly "replay" the changes I did into a new branch, probably because I merged changes from master into my branch now and then.

At the end, I made a diff on master from the state after the merge revert commit to the state before the said commit. So that this diff, when applied, would revert the changes from "merge revert" and thus would reapply my changes. I created a new branch based on the snapshot after merge revert, applied my diff onto it and had my new branch successfully merged into master.

All this worked well, but I wonder if there is a more straightforward way to reintroduce changes from a reverted merge. There were further commits and merges after the reverted merge, so simply restoring the repository from a backup etc. was not an option.

cuckoo
  • 205
  • 3
  • 7
  • 4
    revert the reversal commit – Sergio Tulentsev Jul 26 '21 at 14:16
  • 2
    This has been asked many, many times. For instance: https://stackoverflow.com/questions/8728093/how-do-i-un-revert-a-reverted-git-commit https://stackoverflow.com/questions/5354682/how-can-i-fix-a-reverted-git-commit – IMSoP Jul 26 '21 at 14:47

1 Answers1

1

Sergio's comment is right on target. However, if you really want to see the revisions applied again, there is a trick you can try...... You can reapply the revisions that made the original branch so that they become new revisions and then you can rebase them, or just merge them. Suppose that the original branch is called X and it's made up of 4 revisions (straight revisions, no merges):

git checkout X~4
git cherry-pick HEAD..X # reapply all 4 revisions so that they are brand new
git branch -f X # set X right here
git checkout the-target-branch
git merge X # this should work

You might also try rebasing the branch after the cherry-pick operation.

eftshift0
  • 26,375
  • 3
  • 36
  • 60