4

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.

3
  • 1
    I want to make sure I understand correctly. You have a branch, you merged it into master, then you reverted the code out of master. Now you're trying to merge the same branch into master again? Commented Nov 2, 2018 at 15:03
  • It's not clear what commits you want to keep and which ones your don't? Commented Nov 2, 2018 at 15:10
  • @mkasberg That's exactly what I want to do. Commented Nov 2, 2018 at 15:18

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

2 Comments

How would I write the rebase line for this? Which branch would I work from when I'm rebasing it?
If you really do want to keep your original commits and want to merge them again, there is another option. You can install a temporary graft that hides the second parent of the merge commit. Then a git merge will merge the changes again. You need to know what you are doing, though, and remove the graft as soon as possible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.