Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge after reverting the original merge

Tags:

git

Alright I was inattentive when I was merging some code into a repository(There were major changes in the flow of some code), so I had to revert the merge commit. I went home for the day, and now there are more commits on master, and I can't seem to trigger a merge such that I can get my code into master, as in I have the diffs between the 2 files. I was clumsy and pushed to code to origin already.

Here's the one line graph history of the most recent commits:

Commit History

How do I get back a proper merge screen so that I can take care of it?

I was thinking I could add another commit to my branch then try to merge it, but that feels hackish.

like image 925
HSchmale Avatar asked Mar 06 '23 03:03

HSchmale


1 Answers

When code is committed and then reverted on a branch, re-committing the same code will have no effect because of the original revert. For example, say you have one or more commits on branch B, and you revert them with commit R:

master
|
* R
|\
| * B

Merging B into master again will have no effect because git realizes that revert R happened after all the commits in B (even if B is merged again).

There are 2 possible solutions here.

Revert the Revert

One is to revert the revert (call this commit R').

master
|
* R'
|
* R
|\
| * B

The command for this is simple. $ git revert R


Rebase the Branch

The other possible solution is to rebase the entire branch B so that it comes after the revert R (call the rebased branch B'). There are a few different variations of this, but they all depend on rewriting the branch history with new commits that are not present in the history of R.

master
| 
* New merge commit
|\
| * B'
|  \
|   ...
* R
|\
| * B
| ...
|/
* A

The commands for the rebase are as follows:

$ git checkout B
$ git rebase --no-ff A

Linus has some good commentary on this scenario for more reading.

like image 133
mkasberg Avatar answered Mar 15 '23 07:03

mkasberg