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?

2
  • 1
    I was unable to get this working through the accepted answer by @HeyHeyJC on my Win 10 machine running VS Code V1.32.3. I've added my solution as an answer Mar 27, 2019 at 17:28
  • As far as I can see, VS Code doesn't do merges, only 2-way diffs. Mar 6, 2020 at 20:50

6 Answers 6

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.

6
  • 13
    Default path for VS Code is now C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code\Code.exe
    – Ondrej
    Feb 5, 2019 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 .. Mar 28, 2019 at 9: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, 2019 at 18:24
  • 1
    Got it . thanks anyhow. if you want you can read about it here or here 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 . Mar 29, 2019 at 1: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 1, 2019 at 8:45
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

2
  • 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, 2022 at 14:19
  • On Mac use Sourcetree -> Preferences... and as a Merge Command: /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron Sep 15, 2022 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
4

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

2
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.

0

Also a solution for MacBook is here: Atlassian Community

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.