Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get my git merge conflicts back after merging incorrectly?

Tags:

git

merge

I'm trying to merge 2 branches that have a lot of changes in them, several with merge conflicts. I merged the files using git mergetool, but I've subsequently realized that I merged a couple of them incorrectly. I basically want to go back to the conflicted state for those couple files, so I can re-run the mergetool and correct my errors. I don't want to throw away my entire merge, since most of it is correct.

I've tried resetting to my head and then doing git checkout -m other_branch -- my_file to no avail. I ended up resetting to HEAD, getting the file out of the other branch, and just doing git add --patch on the file, only staging what I wanted. But there must be a better way...

like image 503
bdimcheff Avatar asked Nov 04 '09 22:11

bdimcheff


People also ask

How do you undo a merge conflict?

You can use the git reset --merge command. You can also use the git merge --abort command. As always, make sure you have no uncommitted changes before you start a merge.

How do you undo a merge that has been pushed?

Now, if you have already pushed the merged changes you want to undo to your remote repository, you can right-click on the merge commit and select Revert commit from the context menu. You will then be asked if you want to immediately create a commit.


2 Answers

First, check out if you have conflicted state in the index (before resetting to HEAD), via

$ git ls-files --stage --abbrev my_file

You should get something like the following:

100644 257cc56  1        my_file
100644 b7d6715 2        my_file
100644 5716ca5  3        my_file

If you don't get that, you would have to use git update-index, like Charles Bailey said, or use temporary files. If you have that, then

$ git checkout -m my_file

should work (I have checked this).

like image 84
Jakub Narębski Avatar answered Oct 18 '22 14:10

Jakub Narębski


You can do this with git update-index using either the --cacheinfo or --index-info options to remove the 0 entry in the index for a given file and populating the 1, 2 and 3 entries with the base, local and remote versions respectively but it is going to be fiddly.

It's probably going to be easier to extract the various versions to temporary files, and manually run your merge tool, writing the answer to the correct file and adding the result of the successful merge.

e.g.

git show $(git merge-base HEAD MERGE_HEAD):file >base-file
git show HEAD:file >local-file
git show MERGE_HEAD:file >remote-file

Run mergetool manually, writing to file.

git add file
like image 30
CB Bailey Avatar answered Oct 18 '22 12:10

CB Bailey