Comparing files using Visual Studio Code

  • Tips

In the previous post, I showed you how to use Visual Studio to compare 2 files. I also use Visual Studio Code from time to time. And, as every great IDE, Visual Studio Code also have a great diff tool. As with Visual Studio, you can use it to compare 2 versions of the same file if you use a source control. But you can also compare 2 files from your file system.

There are multiple ways to use the Visual Studio Code diff tool:

Comparing files using the User Interface

  1. Open the 2 files in Visual Studio Code
  2. Right click on one file and click "Select for compare"

  1. Right click on the other file and click "Compare file file1 with file2"

You should see the result:

Comparing files using the command line

  • Using Visual Studio Code
"%LOCALAPPDATA%\Programs\Microsoft VS Code\code.exe" --diff file1.cs file2.cs
  • Using Visual Studio Code Insiders
"%LOCALAPPDATA%\Programs\Microsoft VS Code Insiders\Code - Insiders.exe" --diff file1.cs file2.cs

Using Visual Studio Code as a git difftool

git difftool is a Git command that allows you to compare and edit files between revisions using common diff tools. You can use any editor that support diff such as VS Code. Open a command prompt and use the following commands:

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

By the way, you can also use Visual Studio Code as your git editor:

git config --global core.editor "code --wait"

Do you have a question or a suggestion about this post? Contact me on Twitter or by email!

Follow me:
Enjoy this blog?Buy Me A CoffeeDonate with PayPal

Comments

Gabriel -

Thank you for Documenting this!

Dylan Chien. -

Is it possible to generate a compare result ?

Gérald Barré -

@Dylan Chien., I don't think it's possible out of the box. You should suggest the feature to the team. In the meanwhile, you can use the Git Patch extension to generate and apply patch files.

Sumeet Ranka -

Thanks for documenting this.