Reconflict the merge, after incorrect manual merge

2019-04-16 10:33发布

After resolving a merge conflict and committing, it has become apparent that my manual merge conflict resolution was wrong. I have already staged, committed, and pushed the repo. How can I get the conflicting file back into the conflicting state, such that I might resolve it differently?

Note that it is too late for git checkout -m as I've already committed the incorrect merge resolution.

1条回答
▲ chillily
2楼-- · 2019-04-16 11:12

Starting from a clean working directory, I would hard reset back to before the merge, start a new branch, and perform the merge again. You could then perform a diff between the original merge result branch and your new branch to get a patch file to apply on the original branch. Since you have already pushed your result, you will want to create a new commit correcting the merge commit rather then performing a force push after editing the history.

Overly detailed version:

If your current history looks something like this and c is the merge commit you want to change.

a - c - d <-HEAD/master
   /  
  b

Starting from a completely clean working directory.

git branch old_merge <sha1 of c>  // create branch for original merge
git checkout -b new_merge <sha1 of a> // create and checkout branch before merge
// re-merge branch

This will create something like this...

 old_merge
    ∨
a - c - d <-master
  X  
b - c' <-HEAD
    ∧
 new_merge

That's probably really confusing but hopefully you get the idea. Now you can perform a diff between the old_merge branch and the new_merge branch to get the changes you will need to make on master.

git diff old_merge..new_merge

You could use the patch UNIX utility to apply the difference between the two branches after you have checked out master again.

git checkout master
git diff old_merge..new_merge | patch -p1

Hopefully this helps. Let me know if you are confused by anything.

查看更多
登录 后发表回答