How to compare different branches in Visual Studio Code

GitVisual Studio-Code

Git Problem Overview


How do I compare different branches in Visual Studio Code 1.9?

Is it possible?

Git Solutions


Solution 1 - Git

2021 answer

Here is the step by step guide:

  1. Install the GitLens extension: GitLens
  2. Then, Click on Source Control:

Source Control

  1. Click on Search & Compare

Click on Search & Compare

  1. Click on Compare References

Compare References

  1. Select the branches you want to compare:

Select Branches

  1. Now, You can select any file for which you want to see the diff from Search & Compare

View Diff

Solution 2 - Git

Update: As of November, 2020, Gitlens appears within VSCode's builtin Source Control Panel

I would recommend to use: Git Lens.

enter image description here

Solution 3 - Git

UPDATE

Now it's available:

https://marketplace.visualstudio.com/items?itemName=donjayamanne.githistory

Until now it isn't supported, but you can follow the thread for it: GitHub

Solution 4 - Git

Use the Git History Diff plugin for easy side-by-side branch diffing:

https://marketplace.visualstudio.com/items?itemName=huizhou.githd

Visit the link above and scroll down to the animated GIF image titled Diff Branch. You'll see you can easily pick any branch and do side-by-side comparison with the branch you are on! It is like getting a preview of what you will see in the GitHub Pull Request. For other Git stuff I prefer Visual Studio Code's built-in functionality or Git Lens as others have mentioned.

However, the above plugin is outstanding for doing branch diffing (i.e., for those doing a rebase Git flow and need to preview before a force push up to a GitHub PR).

Solution 5 - Git

It's now possible by using the githistory extension.

Here's a small trick though: You can compare the latest commits from each branch and that would be the same as comparing two branches side by side or creating a PR.

Here's how to do that using githistory extension:

  1. Open githistory
  2. Pick the latest commit from your current branch by clicking on "Git Commit Icon" → (Usually it should be latest commit it the list). From the opened dropdown menu click on "Select this commit".
  3. Pick the latest commit from the branch you want to compare to by clicking "Git Commit Icon".
  4. As a result, the dropdown should appear with a few options → Select the last option that says "Compare with SHA" and you'll see the diff.

Solution 6 - Git

If you just want to view the changes to a particular file between the working copy and a particular commit using GitLens, the currently accepted answer can make it difficult to find the file you're interested in if many files have changed between the versions.

Instead, if the file is open, right click on the file's tab (or, if it's not currently open, go to the file explorer in the side bar and right click on the file), then go to Open Changes > Open Changes with Revision... (or Open Changes with Branch or Tag...).

Solution 7 - Git

In the 11.0.0 version released in November 2020, GitLens views are now by default all placed under the source control tab in VSCode, including the Search & Compare view which has the compare branches functionality:

Compare working branch

It can be changed back to the side bar layout in GitLens settings:

Layout settings

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVitaly MenchikovskyView Question on Stackoverflow
Solution 1 - GitManish MenariaView Answer on Stackoverflow
Solution 2 - GitddsultanView Answer on Stackoverflow
Solution 3 - GitVitaly MenchikovskyView Answer on Stackoverflow
Solution 4 - GitprograhammerView Answer on Stackoverflow
Solution 5 - GitskryvetsView Answer on Stackoverflow
Solution 6 - GitbinaryfuntView Answer on Stackoverflow
Solution 7 - GitDamjan OstrelicView Answer on Stackoverflow