183

Today I was trying to use the git mergetool on the Windows command prompt and realized that it was defaulting to use Vim, which is cool, but I'd prefer VS Code.

How can I have Visual Studio Code function as my GUI for handling merge conflicts (or even as a diffing tool) for Git?

Is it possible to setup VS Code to get visuals for a three-way merge?

5 Answers 5

354

Update: As of Visual Studio Code 1.70 Three-way merge with improvements were added. Visuals and further explanations are available if that's of interest to you 😉.

As of Visual Studio Code 1.13 Better Merge was integrated into the core of Visual Studio Code.

The way to wire them together is to modify your .gitconfig and you have two options.

  1. To do this with command line entries, enter each of these: (Note: if on Windows Command Prompt replace ' with ". Thanks to Iztok Delfin and e4rache for helping clarify this.)

    1. git config --global merge.tool vscode
    2. git config --global mergetool.vscode.cmd 'code --wait --merge $REMOTE $LOCAL $BASE $MERGED'
    3. git config --global diff.tool vscode
    4. git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
  2. To do this by pasting some line in the .gitconfig with Visual Studio Code.

    • Run git config --global core.editor 'code --wait' from the command line.

    • From here you can enter the command git config --global -e. You will want to paste in the code in the "Extra Block" below.

        [user]
            name = EricDJohnson
            email = cool-email@neat.org
        [gui]
            recentrepo = E:/src/gitlab/App-Custom/Some-App
        # Comment: You just added this via 'git config --global core.editor "code --wait"'
        [core]
            editor = code --wait
        # Comment: Start of "Extra Block"
        # Comment: This is to unlock Visual Studio Code as your Git diff and Git merge tool
        [merge]
            tool = vscode
        [mergetool "vscode"]
        # Comment: Original way before three-way merge shown commented out
        #    cmd = code --wait $MERGED
        # Comment: For "Three-way merge"
            cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED
        [diff]
            tool = vscode
        [difftool "vscode"]
            cmd = code --wait --diff $LOCAL $REMOTE
        # Comment: End of "Extra Block"
      

Now from within your Git directory with a conflict run git mergetool and, tada, you have Visual Studio Code helping you handle the merge conflict! (Just make sure to save your file before closing Visual Studio Code.)

Accept Incoming Change anyone?

For further reading on launching code from the command line, look in this documentation.

For more information in git mergetool check out this documentation.

28
  • 1
    Does this integration of Better Merge allow you to accept and choose which portion to merge? Or do you still have to remove by hand the extra comments of the conflict like <<<< HEAD and >>>> develop?
    – ghiscoding
    Aug 29, 2017 at 19:22
  • 3
    There is a line above <<<< Head, that gets inserted that lists the options: "Accept Current Change | Accept Incoming Change | Accept Both Changes | Compare Changes" and I believe it inserts this on each section of detected changes in the file. But within a section if you want to custom merge a bit of this and a bit of that by hand, I believe you would make that change in your local and then go with the "Accept Current Change" option. So the work flow does kind of have you take a step back in order to take a step forward. If others resolve this in a different way please post here to educate us Aug 30, 2017 at 20:03
  • 1
    Oh I see it in your print screen now, very neat. Thanks a lot for the explanation and the how to do it. Will happily use it on the next time conflicts gets in the way :)
    – ghiscoding
    Aug 30, 2017 at 23:31
  • 5
    I get the error unknown tool: vscode... I'm pretty sure to call VsCode from command line you have to use code instead of vscode Sep 5, 2018 at 16:02
  • 28
    Also, this is not working for me. It just pops open VsCode and no files are ever brought up Oct 2, 2018 at 22:35
34

I had to replace the double quotes with simple quotes:

  git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'

for it to work properly (with double quotes, $LOCAL and $REMOTE are replaced by their values).

This is needed if you are using Git Bash for Windows instead of Windows Command Prompt.

6
  • 1
    Not for me. I just did this on Windows using the Command Prompt. Perhaps you're using something different? If so, I suggest adding which environment you're using so others with the same environment will know that they'll need to make this change.
    – Todesengel
    Aug 20, 2018 at 21:04
  • 1
    @e4rache and @Iztok-Delfin what you have listed here is helpful content but you've accidentally made it an answer, when it's really a comment. I'm sure you ran into trouble because you didn't have the 50 point on SO to allow you to comment, which is kind of a site work flow problem that should maybe be looked into. Anyway, thanks for contributing, and I added your tip in my answer above. Thanks for helping those who come later and are using Git Bash :^) Aug 23, 2018 at 21:12
  • @eric-d-johnson I totally meant to make a comment instead of a reply. ( sorry, I'm new to this site ) and btw I was using bash on linux. Is there a way to transorm this reply into a comment ?
    – e4rache
    Sep 4, 2018 at 12:34
  • @e4rache I don't know a way to make it a comment, but perhaps a moderator will see this and give us some tips (hint, hint). If you had the 50 points, you could make a comment on the accepted answer, and delete this one, so once you become an SO rock-star with tons of points you can come back here and do whatever housekeeping makes you happy and brings back good memories of how it all started. Sep 5, 2018 at 18:29
  • 1
    @mohamedghonemi Good feedback. I updated the explanation at the top for macOS and Linux now to remember to use single quotes ('s). Jan 16, 2020 at 20:30
25

On top of the excellent existing answer, you should open VS Code in a new window by adding -n to the command line.

So your git config --global --edit looks something like this.

[merge]
        tool = vscode
[mergetool "vscode"]
        cmd = code --new-window --wait $MERGED
[diff]
        tool = vscode
[difftool "vscode"]
        cmd = code --new-window --wait --diff $LOCAL $REMOTE                                                    
3
  • I used these settings, but when I do git diff commit_id1 commit_id2, it doesn't bring up a VS code editor window. It still defaults to the terminal. Am I using the wrong command? Dec 10, 2021 at 13:33
  • @user5965026 have you tried git diftool instead of git diff?
    – mvd
    Dec 13, 2021 at 15:29
  • 1
    Note: use cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED if you have VSCode 1.7+ and want three-way merge UI.
    – Rafs
    Aug 18, 2022 at 10:23
11

Using the manual you can find an interesting argument:

git difftool --help 
-x <command>, --extcmd=<command>
       Specify a custom command for viewing diffs.  git-difftool ignores the configured defaults and runs $command $LOCAL $REMOTE when this option is specified.
       Additionally, $BASE is set in the environment.

With this information you can easily use the following command without touching the git configuration:

git difftool -x "code --wait --diff" 

Similar question here

4

In case if someone want to resolve it in Visual Studio, another option would be doing it through Visual Studio: Team Explorer -> click on Home icon => Settings button => expand Git section => click on Global Settings

enter image description here enter image description here enter image description here

3
  • 7
    Visual Studio is not equivalent to Visual Studio Code. The way settings like this are done are quite different. See stackoverflow.com/a/33798601 for the differences
    – jwd630
    Jan 18, 2020 at 21:00
  • 3
    @jwd630 I know VS code is different than VS. I had this issue in VS but couldn't find a solution for that so I though it would be better to post it in case someone is experincing this problem. No need to down-vote.
    – Daniel B
    Jan 18, 2020 at 21:07
  • 4
    This is an answer to a completely different question for a different product. Jun 12, 2021 at 9:46

Your Answer

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.