Data report"State of code review 2024" is now liveRead the full report

How to compare two files in Visual Studio Code

Kenny DuMez
Kenny DuMez
Graphite software engineer

Comparing two files in Visual Studio Code (VS Code) is a useful feature for developers looking to spot differences between two versions of a file or different files altogether. This capability is essential for tasks such as code review, merging changes, or simply understanding changes over time. This guide will walk you through how to use VS Code to compare two files, ensuring you can efficiently manage and reconcile differences.

Launch Visual Studio Code. Ensure it is updated to the latest version to take advantage of all of the latest features.

You can open the files in two main ways:

  1. Open files directly in the editor: Navigate to 'File' -> 'Open File...' and select your first file. Repeat the process for the second file.
  2. Use the Explorer pane: If your files are part of a folder, open the entire folder in VS Code (File -> Open Folder...), and then click on the files in the Explorer pane to open them.

To compare two files in VS Code:

  1. Open the Explorer pane (if not already open) by clicking on the first icon in the activity bar on the side or by using the shortcut Ctrl+Shift+E.
  2. Find the two files you want to compare in the Explorer pane.
  3. Right-click on the first file you want to compare, and select 'Select for Compare' from the context menu.
  4. Right-click on the second file you wish to compare it to and choose 'Compare with Selected' from the context menu.

VS Code will open a new tab showing both files side-by-side, highlighting the differences:

  • Added lines are indicated with a green background.
  • Removed lines are shown with a red background.
  • Changed lines are displayed with a blue background.

Use the navigation arrows in the top pane to jump between differences or scroll through both files synchronously to review all changes.

If you need to merge changes from one file to another, you can edit the files directly in the compare view. Any changes you make will be reflected in the original files once you save them.

  • Keyboard shortcuts: Learn and use keyboard shortcuts to improve efficiency. For example, Ctrl+Enter jumps to the next difference, and Ctrl+Shift+Enter jumps to the previous difference.
  • Customize settings: Customize the diff settings by going to Preferences -> Settings and searching for 'diff editor'. Here, you can adjust how differences are displayed, such as ignoring whitespace changes.
  • Use extensions: Consider extensions like 'GitLens' that enhance the diff and compare functionalities of VS Code, especially useful if the files are part of a Git repository.

For more information see the official VS Code documentation.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2