mat_boy
mat_boy

Reputation: 13666

Reverting a revert of a merge with old branch ahead of master

This is my situation. On Branch X originally created from master (M) I did some commits. Then I opened a pull request and one colleague merged to master.

Then, this colleague noticed some problems and created a branch RB on which he did a revert of the merge to master.

In the meanwhile, I did another commit in branch X that has been pushed to origin. Now, If I open a pull request from X to master, I see a merge conflict related to my new commit, but all my changes made before the first merge are completely overridden by the merge revert.

X--X--X--X
       \
M-------M-----M
         \    /
         RB--RB

How can I solve this?

Upvotes: 2

Views: 717

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521194

You can use an interactive rebase to selectively remove the merge commit in master which reverted all your changes.

git checkout master
git rebase -i HEAD~4

This will prompt you with a list of commits looking something like this:

pick jd832ng some commit
pick bd381nv merge commit branch RB into master
pick al83mld an earlier commit here
pick f2ui2nd another earlier commit

Delete the line containing the merge commit, leaving you with the following:

pick jd832ng some commit
pick al83mld an earlier commit here
pick f2ui2nd another earlier commit

Now save this file, and complete the rebase. You have removed the merge commit from the master branch.

Note:

If the merge commit from branch RB is the most recent commit in master, then you can nuke it using the following:

git checkout master
git reset --hard HEAD~1

This will remove the head of the master branch. If this happens to be the merge commit, then you are in luck and you can avoid the interactive rebase. If, on the other hand, other people have made commits on top of the merge, then you are pretty much left with interactive rebase as an option.

Upvotes: 2

Related Questions