简体   繁体   中英

git: 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?

ADDING:

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")

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

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

i had the same issue,

a branch was merged in dev, which erased another branch changes during conflict resolution.

i've used cherry-pick for that :

git cherry-pick {merge commit sha of missing branch code} -m 1

i was able to redo the whole conflict resolution process.

jthill's answer was exactly what I was looking for, but I thought I'd point out that if you are one of those who prefers git restore to git checkout , the equivalent commands are:

git restore --merge -- <filepath>

and

git restore --conflict=diff3 -- <filepath

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM