313

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

Is it possible?

littleironical
  • 1,141
  • 1
  • 7
  • 22
Vitaly Menchikovsky
  • 6,164
  • 16
  • 53
  • 88

7 Answers7

576

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

Manish Menaria
  • 17,647
  • 3
  • 18
  • 23
  • 121
    your highlights are out of the world :D – Vijay Rajpurohit Sep 09 '19 at 06:32
  • 2
    Really Insightful :-) – Himanshu Sharma Sep 09 '19 at 06:32
  • 7
    Your step by step screenshots is very helpful. Thanks a lot. – Anil Tallam Sep 17 '19 at 08:28
  • 1
    This has to be one of these best extensions for VSCode. Nice features! I can even edit the appropriate files once I get to this point in split mode. – klewis Jan 18 '20 at 05:30
  • Note that it only compares the remote version, local changes do not effect to the comparison instantly – cagcak Mar 26 '20 at 09:41
  • 5
    Note that, there are actually two steps in step 4.: select branch A and select branch B to compare with. – Louis Yang Apr 22 '20 at 01:29
  • This extension serves two purposes: do a lot of amazing things with git and learn how to NOT name an extension (I've always ignore it because I tought it was just a simple Lens extension (i.e.: just add some text above changes or something like that)) – JCKödel May 19 '20 at 03:08
  • And is it possible to see conflicts somehow without performing a `git merge`? – Sasuke Uchiha May 26 '20 at 10:33
  • Is the ability to compare two branches available in the "commercial" Visual Studio Community/Pro/Enterprise or is it only available in third party products like Visual Studio Code, Git Bash/Kdiff3 and Tortoise Git? – Shawn Eary Nov 12 '20 at 15:13
  • The GitLens features can now be found under the "Source Control" panel in VSCode. The dedicated GitLens panel somehow disappeared – Tracer69 May 19 '21 at 07:35
  • @Tracer69 Thanks for pointing it out. I will update the answer when I will have some free time – Manish Menaria May 19 '21 at 09:36
  • Thank you @ManishMenaria, I have been honestly looking for a nice way of doing this and this falls close to my workflow as I use VSCode a lot – leeroya Jul 16 '21 at 11:22
  • is there a way to paste in a commit hash? it only allows typing - it autocompletes, yes, but it'd be a lot easier to use VSCode's `copy commit hash to clipboard` feature and paste it in. – Max Cascone Jul 27 '21 at 20:40
  • 1
    `F1` > `gitlens: compare` is helpful if you don't like the sidebar – gebbissimo Nov 16 '21 at 10:37
  • `Contributors` and `Search & Compare` panels may be hidden. You can enable them by clicking the ellipsis menu at top of Source Control sidebar – Adam Dec 10 '21 at 16:27
  • Before you compare the branches, make sure you pull both the branches. If this step is missed then there may be unexpected files or content during comparison. – Sandeep Amarnath Mar 22 '22 at 15:11
  • what does "highlights" mean? @VijayRajpurohit – cryanbhu Apr 19 '22 at 04:12
  • I just installed GitLens five minutes ago based on your answer and didn't realize how much I needed this extension in general – Frank A. May 11 '22 at 20:39
  • Strangely when I do this, I very rarely find that some branches are missing and not able to be compared. Is there a reason for that? – openCivilisation May 21 '22 at 03:42
  • @openCivilisation Maybe try running `git fetch --all` to fetch all remote branches? – Manish Menaria May 25 '22 at 09:50
94

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

ddsultan
  • 1,755
  • 17
  • 16
  • 38
    To add some details on usage, the way I found to compare branches in Git Lens is to; Open the Explorer view (Ctrl + Shift + E), find the Git Lens group, right click the branch you want to compare and select 'Select for Compare',then right click the second branch and select 'Compare with Selected'. The results will show up as a seperate group called GitlensResults below Git Lens. There you can look at commits and compare files directly. – Vidar Apr 19 '18 at 19:20
  • 1
    I learned of GitLens last year (2017) and was impressed immediately. The GitLens view at the bottom you select the branch you want to compare, and then find the branch you want to compare it against as stated above "Compare with Selected" - and then you get to select the individual files that show up as changed/added between the two. This extension is the first one I install on a new install of VSCode. I rely on it. – Mark W. Mitchell Nov 13 '18 at 16:44
  • I would like to compare current working tree with certain branch, and edit files directly. GitLens allows me to Save as... the actual file, but I need to find it first. Is there a more convenient way? In IntelliJ's IDEA I just click an arrow and move those changes... – Radim Vansa Jan 21 '19 at 09:11
  • 1
    GitLens is so easy to use, it is very helpful. Thanks for sharing. – R.F Mar 15 '19 at 03:51
  • 1
    Best answer I was struggling to get Git History Diff to work and this one is SO EASY to use. Cannot recommend enough thanks for the info! – Lostaunaum Aug 28 '19 at 15:40
  • What do you mean by "find the Git Lens group"? – Snowcrash May 23 '22 at 09:07
16

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Vitaly Menchikovsky
  • 6,164
  • 16
  • 53
  • 88
15

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).

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
prograhammer
  • 18,956
  • 13
  • 86
  • 115
8

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.
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
skryvets
  • 2,500
  • 26
  • 30
8

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...).

binaryfunt
  • 5,618
  • 5
  • 33
  • 54
  • 3
    Exactly what i was looking for (compare 1 file between 2 branches), Thank you ! – rdhainaut May 05 '21 at 10:36
  • This is also just what I needed. One minor point: the "Open Changes" option is available when right clicking on the file in the _Source Control_ panel, rather than the _File Explorer_ panel (and after installing the Git Lens extension I think). – ianinini Jan 09 '22 at 23:31
2

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

Damjan Ostrelic
  • 882
  • 4
  • 12