Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This question and some others discuss how this could work on a Mac, but I couldn't find documentation from Microsoft or Atlassian on a way to do this on Windows without trying to make it the global Git setting. (I just prefer to change things within the IDE where possible.)

What are the parameters to use with the "Custom" settings in SourceTree's "Options" dialogue "Diff" tab, for diff'ing and merging with VS-Code?

like image 727
HeyHeyJC Avatar asked Apr 23 '18 13:04

HeyHeyJC


People also ask

How do I set the diff tool in SourceTree?

SourceTree config First, open up the options window and go to Diff tab. Change both External Diff Tool and Merge Tool to Custom. In the Diff Command field enter the full path to the vsdiffmerge.exe. For VS 2015 and below you can find it in the Visual Studio installation folder, under Common7\IDE subfolder.

What is diff editor VSCode?

Diffing in VS Code is very useful for quickly seeing changes between two files. It also helps to remind yourself of the changes you've made from the master version of a file on git once in a while. VSCode diffs are a great thing to add to your developer toolbox.


3 Answers

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.

like image 125
HeyHeyJC Avatar answered Sep 20 '22 00:09

HeyHeyJC


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
like image 45
Dan Gardner Avatar answered Sep 19 '22 00:09

Dan Gardner


You need to do below configuration in Sourcetree

  • Go to SourcetreeToolsOptionsDiff

  • In the section External Diff / Merge, select Custom for the External Diff Tool and Merge Tool

  • Diff Arguments → --diff --wait "$LOCAL" "$REMOTE"

  • Merge Arguments → -n --wait "$MERGED"

  • After configuration is saved, then goto the Sourcetree and right click on the file with merge conflicts Resolve ConflictsLaunch External Merge Tool

Note: Diff and Merge command should point to C:\Users\<user name>\AppData\Local\Programs\Microsoft VS Code\Code.exe

Sourcetree external diff/merge tool configuration

Step to resolve conflict in source tree

like image 35
Swapnil Avatar answered Sep 18 '22 00:09

Swapnil