Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redo bad git conflict resolution after push

I want to re-create a merge conflict so I can resolve it correctly the second time round.

Example:

  • Branch 'A' checked out.
  • Branch 'B' is merged In.
  • Conflicts resolved from merge (creates merge commit).
  • Push to remote.
  • Other people merge into Branch 'A' and push to remote.
  • Oh dear I have realised my conflict resolution was wrong, I went with theirs instead of mine, whatever.
  • Now what?

I essentially want to re-do the conflict resolution part.

I don't have the option of re-setting my HEAD as the branch has already been pushed to the remote; and has the possibility of other people having committed on it, before I had realised the conflict resolution was wrong.

I also want to avoid making a direct fix on branch 'A'.

I want to avoid cherry picking. I know I can do a standard revert and cherry pick my commits etc, I don't want to do this.

So is there any graceful way of doing this?

I have tried reverting the merge commit then reverting the revert and merging branch 'B' back in again, but unfortunately it does not ask me to resolve any conflicts the second time round, I just get the standard 'Already up to date' message.

Putting it simply, I want to re-create my conflict so I can resolve it correctly the second time round.

Any help will be much appreciated.

Thanks.

like image 278
Arthurion Avatar asked Mar 24 '17 18:03

Arthurion


People also ask

How do you revert conflict?

On the command line, a simple "git merge --abort" will do this for you. In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.

How do I undo an unfinished merge?

You can use the git reset --merge command. You can also use the git merge --abort command.


1 Answers

There's two steps here. First is recreating the merge and conflict. The second is applying it to the new tip of the branch.

You have something like this.

1 - 2 - 5 - 6 - 7 - 9 - 10 [A]
     \         /
      3 - 4 - 8

7 is the merge commit and you want to redo that and apply the fixes on top of A. Rebasing might get messy because too much work has been piled on top.

First, let's recreate the merge conflict. To do that we'll just do it over again. Checkout 6, that's where A was when you merged, and merge it with 8.

git checkout 6
git merge 8

You'll have to use git log --graph to determine the actual commit ids. Now we have the merge conflict. Resolve it as you would but don't commit it. Instead, stash it. git stash save. This will save the diff off in a corner called "the stash". This is just a more formal way of saving patches.

Now that we've got the conflict resolution as it would have happened, checkout A and apply the fix from the stash.

git checkout A
git stash pop

Since there's been changes to A, you may get fresh conflicts out of this. That's fine. Resolve them normally and commit.

like image 175
Schwern Avatar answered Oct 04 '22 19:10

Schwern