49

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?

HeyHeyJC
  • 2,627
  • 1
  • 18
  • 27

6 Answers6

85

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.

HeyHeyJC
  • 2,627
  • 1
  • 18
  • 27
  • 13
    Default path for VS Code is now `C:\Users\\AppData\Local\Programs\Microsoft VS Code\Code.exe` – Ondrej Feb 05 '19 at 10:16
  • is this a real 3-way diff like git require or just a simple 2-way? maybe this is why the intermediate file remains .. – Obmerk Kronen Mar 28 '19 at 09:42
  • 3
    @ObmerkKronen Sorry, the reason I use Sourcetree is because I don't know what things like '3-way diff like git require' means. I use it exclusively with git repos if that helps? – HeyHeyJC Mar 28 '19 at 18:24
  • 1
    Got it . thanks anyhow. if you want you can read about it [here](https://stackoverflow.com/questions/47115431/what-are-two-and-three-way-diff-merge) or [here](https://stackoverflow.com/questions/4129049/why-is-a-3-way-merge-advantageous-over-a-2-way-merge) for example. 3-way merge is what git uses (or should use). your answer has only 2 arguments so I assumed it's 2-way ( the third is the leftover file that as you stated sourcetree does not know to handle .maybe adding anoteer argument would do the trick . – Obmerk Kronen Mar 29 '19 at 01:45
  • 2
    Thanks for the answer, it works fine for me, It would be more helpful if I can setup shortcuts to move between conflicts, in vs-code, any suggestion to do this. – fizmhd Apr 01 '19 at 08:45
  • To assign a key shortcut look in the shortcut editor for workbench.action.compareEditor.previousChange and nextChange. – Russell Owen Aug 13 '21 at 23:48
10

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 %LOCALAPPDATA%\Programs\Microsoft VS Code\Code.exe (in case it is installed for your user only), or C:\Program Files\Microsoft VS Code\Code.exe (in case it is installed for all users).

Sourcetree external diff/merge tool configuration

Step to resolve conflict in source tree

Swapnil
  • 1,004
  • 1
  • 13
  • 21
  • the arguments --diff --wait are required for the diff command to work now, if you had set this up previously without them – Chicowitz Mar 25 '22 at 14:19
  • On Mac use Sourcetree -> Preferences... and as a Merge Command: /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron – Grzegorz R. Kulesza Sep 15 '22 at 11:53
8

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dan Gardner
  • 1,069
  • 2
  • 13
  • 29
4

On macOS it is sufficient to put code in the "Diff Command" box. Then use the arguments specified above by HeyHeyJC

Russell Owen
  • 393
  • 2
  • 14
  • ```code``` seem to not work on macOS. Instead write ```/usr/local/bin/code``` as explained here: https://stackoverflow.com/a/61897895/373542 – Giorgio Oct 01 '21 at 09:08
  • Prerequisite for this is to enable `code` keyword for CLI/Terminal refer here on the integration https://code.visualstudio.com/docs/setup/mac – Dan Aug 03 '23 at 11:32
0

You can also keep on open bouth Sourcetree and Visual Studio with opened project that you works on. And when conflict occurs switch to Visual Studio Git Changes View. Visual Studio and other tools like Sourcetree synchronize with current repository state so when you checkout to branch or make commit in other tool it displays in Visual Studio and vice versa. Same with conflicts. And when you resolve conflict in VS this will be displayed in Sourcetree. If you lazy like me, this can solve your problem without configuring anything.

Vlad
  • 3
  • 4
0

Also a solution for MacBook is here: Atlassian Community

Grzegorz R. Kulesza
  • 1,324
  • 16
  • 10