Git - Using Visual Studio Code as your merge tool

If you like using an editor instead of a 3-way merge tool to edit merge conflicts then VS Code is an option for integrating with git. Here is how I've set this up in the past.

Configuration VS Code

If you have a large number of conflicts and run git mergetool you will find that you bog your system down opening up a new instance of VS Code for every file that has a merge conflict. In order to avoid this, you can tell VS Code to open new files in a new tab and not a new instance of VS Code by adding the setting "window.openFilesInNewWindow": false to your VS Code user preferences.

Configuring Git

In order to use VS Code to resolve conflicts you first need to tell git to use VS Code as it's editor. You do this by running:

git config --global merge.tool code

Next you have to tell git how to call VS Code when doing a merge:

git config --global mergetool.code.cmd "code $MERGED"

Also, since you're going to be resolving the conflicts in an editor and not a merge tool you don't need git to create *.orig files while resolving merges so you can prevent that by running:

git config --global mergetool.keepBackup false

Lastly, because we are going to be resolving the merge conflict in VS Code, we want to tell git that when we close the file (after we've resolved the conflict) to assume that the conflict is in fact resolved without asking us. To set that up just run:

git config --global mergetool.code.trustExitCode true

Resolving Conflicts

With that in place you can now work on a single merge conflict at a time by running:

git mergetool <file>

Or you can tell git to open VS Code for every merge conflict by running:

git mergetool

WARNING

Until VS Code implements a feature where it will re-open tabs upon restart if you close VS Code then you will have to reset your branch and start the merge over because git will think the conflicts have been resolved but VS Code will not remember which files were open for editing when it starts back up.

Wes Shaddix

Read more posts by this author.