Search code examples
gitmergerevert

how to redo bad merge but keep some of my later commit?


the history will be like this:

A<───┬─ BadMerg <── C  <── D
B<───┘

Now I realized the conflict resolution in the merge was bad. I want to redo the merge again, but to keep the commit C and D. How can I do this?


Solution

  • The easiest end riskless way that comes to my mind is:

    1 - create a new branch named "backup" on commit D but DO NOT checkout to that branch. Stay on your current one.

    2 - hard reset your current branch to either commit A or B (pick A if you want to merge B into A)

    3 - merge B

    4 - cherry pick C and D

    5 - if everything is good, delete your backup branch.