Visual diff in Git

If you need to solve a conflict in git but for some reason you can't use the git markers to decide what to keep and what to remove from a file, then you can use git show and your favorite visual diff tool to solve the conflict.

First, get the file as it was in the three stages, respectively common to both branches, the HEAD version and the MERGE_HEAD version:

git show :1:file.txt > /tmp/1.txt
git show :2:file.txt > /tmp/2.txt
git show :3:file.txt > /tmp/3.txt

Then, use a visual diff tool to compare the three files and get them all to look the same. For example, you can use vim -d as a visual diff tool:

vim -d /tmp/{1,2,3}.txt

After this operation, the files should be identical and should contain the correct version. You can now simply replace the contents of the original file with the corrected version:

cat /tmp/3.txt > file.txt
rm /tmp/{1,2,3}.txt