Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 728
user1934428 Avatar asked Apr 13 '15 14:04

user1934428


People also ask

How do I undo a merge conflict resolution?

On the command line, a simple "git merge --abort" will do this for you. In case you've made a mistake while resolving a conflict and realize this only after completing the merge, you can still easily undo it: just roll back to the commit before the merge happened with "git reset --hard " and start over again.

How do you resolve a merge conflict without committing?

Let us do what Git suggests without performing any useless commits: Resolve the conflict(s) manually or using some merge tool. Then use git reset to mark conflict(s) as resolved and unstage the changes. Also you can execute it without any parameters and Git will remove everything from the index.

How can I redo a merge?

You can use the Git reset command to undo a merge. Firstly, you need to check for the commit hash (or id) so you can use it to go back to the previous commit. To check for the hash, run git log or git reflog . git reflog is a better option because things are more readable with it.


2 Answers

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>
like image 172
Parker Coates Avatar answered Oct 13 '22 20:10

Parker Coates


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
like image 30
jthill Avatar answered Oct 13 '22 22:10

jthill