Amitabh
Amitabh

Reputation: 61337

Use Visual Studio Code as a Git diff tool

Visual Studio Code has a nice built-in feature to diff two files.

Is it possible to use Visual Studio Code diff as the diff tool for Git?

Upvotes: 17

Views: 13905

Answers (3)

successhawk
successhawk

Reputation: 3361

Here is the official documentation:

Visual Studio Code as Git difftool and mergetool

Content as of 2022-11-16 copied below, in case it moves:

You can use VS Code's diff and merge capabilities even when using Git from command-line. Add the following to your Git configurations to use VS Code as the diff and merge tool:

[diff] tool = default-difftool [difftool "default-difftool"] cmd = code --wait --diff $LOCAL $REMOTE [merge] tool = code [mergetool "code"] cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED

This uses the --diff option that can be passed to VS Code to compare two files side by side. The merge tool will be used the next time Git discovers a merge conflict.

To summarize, here are some examples of where you can use VS Code as the editor:

  • git rebase HEAD~3 -i do interactive rebase using VS Code
  • git commit use VS Code for the commit message
  • git add -p followed by e for interactive add
  • git difftool <commit>^ <commit> use VS Code as the diff editor for changes

Upvotes: 1

Maciej Pokrzywiński
Maciej Pokrzywiński

Reputation: 101

Yes, it’s possible. You just must set up Visual Studio Code as your default difftool by adding this in your ~/.gitconfig file:

[diff]
    tool = vscode
[difftool "vscode"]
    cmd = code --wait --diff $LOCAL $REMOTE

After this operation, just run a command, for example: git difftool master. After a few seconds, Visual Studio Code runs difftool.

Upvotes: 5

Max Coplan
Max Coplan

Reputation: 1503

Like Maciej says, gitconfig is the way to go. With this I can set it up to be not just a difftool, but also the merge tool for Git.

[diff]
    tool = vscode
[merge]
    tool = vscode
[difftool "vscode"]
    cmd = code --wait --diff $LOCAL $REMOTE
[mergetool "vscode"]
    cmd = code --wait $MERGED

I use Visual Studio Code Insiders, to get the latest (but still stable) features ahead of time.

[diff]
    tool = vscode
[merge]
    tool = vscode
[difftool "vscode"]
    cmd = code-insiders --wait --diff $LOCAL $REMOTE
[mergetool "vscode"]
    cmd = code-insiders --wait $MERGED

There is now official Visual Studio Code documentation for using Visual Studio Code as both a difftool and a mergetool.

In your ~/.gitconfig file:

[diff]
    tool = default-difftool
[difftool "default-difftool"]
    cmd = code --wait --diff $LOCAL $REMOTE
[merge]
  tool = code
[mergetool "code"]
  cmd = code --wait --merge $REMOTE $LOCAL $BASE $MERGED

Upvotes: 21

Related Questions