Resetting a file to its pre-merge status

Category: Git
Date: November 28, 2016

Merging git branches can sometimes lead to conflicts, meaning that git is not able to automatically resolve changes made on the same portions of files in the branches. I usually fix this by running git mergetool, which in my cases runs the visual diff editor meld showing you what has been changed in the local and in the remote branches, letting you pick the correct version.

On small code bases this is fairly easy, but in large software projects this step takes a lot of effort. You might need to check for tens of files and properly fix a large amount of conflicts, and making mistakes is easy. For example you might miss one conflict, and the result can be nasty.

What I usually do before committing the merge is to test the software by compiling and running it. If something goes wrong, I know that (usually) there has been a problem in the merge procedure. After finding the problem you try to fix it, but sometimes you want to perform git mergetool again on the set of files that caused the problem, without restarting the merge process from scratch. The solution in this case is to run

git checkout -m <list of files>

which will restore the status of the files to their pre-merge status. By running git mergetool again, the assisted merge procedure for the chosen files will restart.