How to use VS Code as merge and diff tool in SourceTree?

I was able to set up with the following steps using the code.cmd script:

  • Setting "Custom" as the external tool for both diff and merge
  • Pointing to the code.cmd command

    C:\Users\[username]\AppData\Local\Programs\Microsoft VS Code\bin\code.cmd

  • Using the same command-line arguments as @HeyHeyJC explains

    The command-line arguments for Diff'ing are:

    --diff --wait "$LOCAL" "$REMOTE"

    And for Merging:

    -n --wait "$MERGED"

For me I was then able to use both diff and merge from Sourcetree.


Note: By default the code.cmd file contained a command line argument to open the CLI instructions "%~dp0..\resources\app\out\cli.js", and having this open each time was a bit tedious! I removed this so my file looked as follows, and now only the files sent from Sourcetree are opened.

@echo off
setlocal
set VSCODE_DEV=
set ELECTRON_RUN_AS_NODE=1
call "%~dp0..\Code.exe" %*
endlocal

There may be other better variations, but after some experiment I've found these work well enough...

  • Choose "Custom" for the "External Diff Tool" and "Merge Tool"
  • The "Diff Command" in both cases is the path to wherever VS-Code is installed, eg:

    C:\Program Files (x86)\Microsoft VS Code\Code.exe

  • The command-line arguments for Diff'ing are:

    --diff --wait "$LOCAL" "$REMOTE"

  • And for Merging:

    -n --wait "$MERGED"

The '-n' flag makes VS-Code open a new window, which I prefer since VS-Code opens so fast, but you can omit it. When dealing with merge conflicts, you have to close the file when you're done to continue. SourceTree doesn't seem to consistently delete the intermediate files it creates, but you can select, right-click and 'Remove' them from the un-staged file section easily enough.