gitmerge

How to redo a merge conflict resolution (before committing the merge)?


I did a

git merge FETCH_HEAD

and, after git told me that there is a conflict in one file, I did a

git mergetool

which in my case runs SourceGear DiffMerge as a GUI mergetool. Immediately after saving the merged file, I realized that I made a bad mistake. I just want to forget the merge and do it all over.

Since I didn't have executed a "git add" yet, let alone committed anything, I thought I could erase my mistake and redo the merge easily like this:

git reset FILENAME
git merge FETCH_HEAD
git mergetool

This does not work. "git merge" by this time tells me

fatal: You have not concluded your merge (MERGE_HEAD exists).
Please, commit your changes before you can merge.

But of course I don't want to commit the screwed-up merge. "git mergetool" complains

No files need merging

I guess I made a mistake at the "git reset" command. What is the proper way to do this?

I did

git merge --abort

then again:

git merge FETCH_HEAD

This yielded

error: Your local changes to the following files would be overwritten by merge: ...

git status

says:

On branch ....
Your branch is up-to-date with ......
Changes not staged for commit:
    modified:   nVP.ini
Untracked files:
    nVP.ini.orig
no changes added to commit (use "git add" and/or "git commit -a")

Would simply a git checkout of nVP.ini bring back the situation before the merge?


Solution

  • To undo a bad conflict resolution before committing, git checkout -m -- $thefile.

    $ git merge 3fac3
    Auto-merging file.txt
    CONFLICT (content): Merge conflict in file.txt
    Automatic merge failed; fix conflicts and then commit the result.
    $ vi file.txt         # fix all the conflicts. do it wrong.
    $ git add file.txt    # <--- ooops
    $ git checkout -m -- file.txt    # <--- the correct un-oops-ifier here
    

    and file.txt is back to the failed-automerge state.

    Note that you can also specify the conflict-reporting style, so if you ordinarily do the default two-diff style but you've got a baffling one and want to see the original too, you can do

    git checkout -m --conflict diff3 -- file.txt