461

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

Is it possible?

littleironical
  • 1,593
  • 1
  • 11
  • 25
Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89
  • 1
    No, but you should look for or file a feature request on github. – Rob Lourens Feb 08 '17 at 16:51
  • [Git Tree Compare](https://marketplace.visualstudio.com/items?itemName=letmaik.git-tree-compare) if you just want a simple plugin to diff files between working tree and a chosen branch. – Noam Manos Jun 09 '23 at 00:26

9 Answers9

825

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
  • 23,899
  • 3
  • 21
  • 23
  • 3
    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
  • 13
    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
  • 1
    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
  • 3
    `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
  • 1
    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
  • 4
    I just installed GitLens five minutes ago based on your answer and didn't realize how much I needed this extension in general – WTFranklin 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
  • 1
    @openCivilisation Maybe try running `git fetch --all` to fetch all remote branches? – Manish Menaria May 25 '22 at 09:50
  • @ManishMenaria: That will update the remote refs (so you can compare `origin/my-branch`) but won't merge the remote ref into your local copy of the branch (so comparing to `my-branch` won't have the changes). Neither is particularly right or wrong, it depends exactly what you want to see. – Tom Jul 06 '22 at 12:37
  • 1
    Does git lens allow you to select the changes you could accept, like on a line by line basis... as you do in intellij? If not, is there any other extension that does this? Thanks – Juan M Jul 26 '22 at 14:45
  • This is amazing! Even works for Jupyter Notebooks. – rusheb Mar 05 '23 at 14:16
  • 11
    as of June 2023 - search and compare option is available in Gitlens tool bar on the left, rather than Git – Danish Jun 16 '23 at 02:49
  • There is no search and compare option. – Soerendip Jul 11 '23 at 01:16
109

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
  • 2,027
  • 1
  • 19
  • 19
  • 44
    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
  • 1
    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
  • paid extension be aware of that!!!! – ANIK ISLAM SHOJIB Dec 22 '22 at 06:26
  • I don't see gitlens anywhere in that screenshot... – john k Feb 10 '23 at 21:26
  • @johnktejik, > Gitlens appears within VSCode's builtin Source Control Panel – ddsultan Feb 14 '23 at 11:15
26

This is possible (and much simpler) without GitLens, using only the Git Graph plugin.

With your Git Graph open, click on the first commit you want to compare, and then Ctrl/Cmd + Click on your second commit. Both lines will be highlighted. The commit details will now display all differences between A and commit B, allowing you to compare any two commits across branches or from the same branch.

enter image description here Source: Git Graph extension page

Drack
  • 379
  • 3
  • 4
22

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 Commit Changes > Open Changes with Revision... (or Open Changes with Branch or Tag...).

Ariel Gabizon
  • 2,851
  • 2
  • 17
  • 22
binaryfunt
  • 6,401
  • 5
  • 37
  • 59
  • 5
    Exactly what i was looking for (compare 1 file between 2 branches), Thank you ! – rdhainaut May 05 '21 at 10:36
  • 2
    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
17

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,738
  • 21
  • 105
  • 131
prograhammer
  • 20,132
  • 13
  • 91
  • 118
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,738
  • 21
  • 105
  • 131
Vitaly Menchikovsky
  • 7,684
  • 17
  • 57
  • 89
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,738
  • 21
  • 105
  • 131
skryvets
  • 2,808
  • 29
  • 31
4

I typically want to compare the current checked out state with some other branch, often master. As I like using keyboard shortcuts, the easiest way I found (compiled from the various answers already present):

  1. Press ctrl/cmd-P, to open the VSC command palette
  2. Start typing 'git compare'. The following options appear: enter image description here
  3. Select 'Compare HEAD with...' (or any other that better suits your use-case)
  4. In the dialog box that appears, select the desired branch, e.g. master enter image description here
  5. Check the result in the 'Search & Compare' window (typically, bottom-left). Expand the arrows as needed, e.g. to see the files that are different, or the Behind/Ahead to see the list of commits. enter image description here

If instead you want to compare two specific branches (and not the checked out one), select 'Compare References...' in step 3. After that you can select the 2 branches/tags/commits/...

Rabarberski
  • 23,854
  • 21
  • 74
  • 96
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

Dostrelith
  • 922
  • 5
  • 13