Search code examples
gitgit-merge-conflict

How to move a file back into the conflict state after conflict resolution?


I have a real bugger of a conflict resolution set, and I figured that I'd just go for it and if I failed, then I should be able to bring back the file into its conflict state. However, I can't seem to find a way to go back. git is still in MERGING state, so how do I backup a single file and reresolve?


Solution

  • First, if you think you might have a decent resolution (or part of it), copy the file somewhere else. :-) Then just run:

    git checkout -m -- path

    (the -- part is needed only if the file's path resembles a valid git checkout option, e.g., if the file you want to put back in conflicted state is named --theirs or -f or some such). This will re-create the conflict-marker variant of the file from the three inputs (merge base and both branch heads), and write that file to the work-tree.

    Note that you can specify the --conflict=merge (default) style, or the --conflict=diff3 style. This is the same as setting merge.conflictStyle in .gitconfig, but just for that one file and this one checkout.

    If you (or git mergetool) used a merge tool that already ran git add on the resolved variant, note that this puts the file back into conflicted state, i.e., it restores the three higher stage entries into the index. You can now run git mergetool again, or—my personal preference—just manually edit the conflicted file and git add the resulting work-tree copy once you are satisfied that it is correct.