Configuring Visual Studio as your Git mergetool

Configuring Visual Studio as your Git mergetool can help people familiar with it to resolve conflicts more easily, here I show you how.

The default option for the Git mergetool is vimdiff, which although perfectly fine, will be unfamiliar to a lot of people, particularly those with a .NET development background. For this reason I’ve changed my config to use the vsdiffmerge component of Visual Studio to do my Git diffs and merges.

Visual Studio Code as default editor

First of all, you may want to change the default Git editor to be Visual Studio Code. This will be used for commit messages if you leave off the -m command line switch when calling git commit. So to enable it, run:

git config --global core.editor 'code --wait'

The --wait option will make the parent process wait for us to close Code before continuing.

The reason I suggest doing this first is that we will be using this editor to edit the config to add VS as a mergetool. To go ahead with this, open the global config with:

git config --global -e

Visual Diff Merge as mergetool

To configure the vsdiffmerge utility as your mergetool, add the following sections to your .gitconfig (open in VS Code after the previous command):

[merge]
    tool = vsdiffmerge
[mergetool]
    prompt = true
[mergetool "vsdiffmerge"]
    cmd = \"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\IDE\\vsdiffmerge.exe\" \"$REMOTE\" \"$LOCAL\" \"$BASE\" \"$MERGED\" //m
    keepbackup = false
    trustexistcode = true

You may need to change the cmd, depending on the location of your Visual Studio installation.

Invoking Visual Studio Diff Merge

So next time you perform a merge and it has conflicts, you can start to resolve the conflicts with Visual Studio by entering git mergetool in the conflicted repository. As we specified the prompt = true option you will be prompted to resolve each conflicted file.

Hopefully this post has helped with your Git merging!

Leave a Reply

Your email address will not be published. Required fields are marked *