How to compare different branches in Visual Studio Code
Asked Answered
T

11

521

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

Is it possible?

Tirol answered 8/2, 2017 at 11:59 Comment(2)
No, but you should look for or file a feature request on github.Crematorium
Git Tree Compare if you just want a simple plugin to diff files between working tree and a chosen branch.Belaud
A
885

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

Antre answered 7/9, 2019 at 13:5 Comment(23)
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.Khajeh
Note that it only compares the remote version, local changes do not effect to the comparison instantlyPlutonic
Note that, there are actually two steps in step 4.: select branch A and select branch B to compare with.Visage
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))Personable
And is it possible to see conflicts somehow without performing a git merge?Rochellrochella
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?Galanti
The GitLens features can now be found under the "Source Control" panel in VSCode. The dedicated GitLens panel somehow disappearedXylograph
@Xylograph Thanks for pointing it out. I will update the answer when I will have some free time 🙂Antre
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 lotAffettuoso
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.Acriflavine
F1 > gitlens: compare is helpful if you don't like the sidebarFryer
Contributors and Search & Compare panels may be hidden. You can enable them by clicking the ellipsis menu at top of Source Control sidebarLightsome
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.Intermixture
what does "highlights" mean? @VijayRajpurohitClosestool
I just installed GitLens five minutes ago based on your answer and didn't realize how much I needed this extension in generalCercus
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?Hols
@Hols Maybe try running git fetch --all to fetch all remote branches?Antre
@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.Dinothere
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? ThanksExhilaration
This is amazing! Even works for Jupyter Notebooks.Nod
as of June 2023 - search and compare option is available in Gitlens tool bar on the left, rather than GitAcetylide
There is no search and compare option.Plantain
@ManishMenaria I am going to thank you a thousand times! This is one of the greatest features of Git and VSCode, I have ever seen! Absolutely first class...Mauldon
A
115

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

Alexandra answered 30/10, 2017 at 10:19 Comment(9)
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.Considering
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.Nd
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...Quadruplet
GitLens is so easy to use, it is very helpful. Thanks for sharing.Cassicassia
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!Paleolithic
What do you mean by "find the Git Lens group"?Giraffe
paid extension be aware of that!!!!Unmask
I don't see gitlens anywhere in that screenshot...Viipuri
@johnktejik, > Gitlens appears within VSCode's builtin Source Control PanelAlexandra
A
61

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

Atalie answered 30/11, 2022 at 8:34 Comment(0)
Z
25

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

Zela answered 22/1, 2021 at 16:8 Comment(3)
Exactly what i was looking for (compare 1 file between 2 branches), Thank you !Semitic
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).Wellmeaning
It's insane that such a useful and very common workflow would be so obscurely named. I used to know how to do it, but then had to google it again for 15 minutes, because nothing I have tried gave me this.Slew
L
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).

Larimore answered 7/8, 2018 at 16:51 Comment(1)
For some reason, I can't get "GitHD" to appear in the F1 / command paletteWonacott
T
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

Tirol answered 10/2, 2017 at 19:59 Comment(0)
J
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.
Jeer answered 2/5, 2018 at 18:43 Comment(0)
O
6

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

Osteo answered 31/1, 2023 at 12:17 Comment(0)
S
4

Looks like there's an update for git lens.

Here's the new guide (2024), It'd take just a minute:

  1. Install the GitLens extension for vscode.
  2. Right click on one of the sideBar items.
  3. On the opened window, click GitLens (there should be 2 gitLens options, make sure you click the upper one and click the other if that didn't work):

enter image description here

  1. Now you should see a new icon added somewhere in the sideBar, click on it:

enter image description here

  1. On the open window you can see 'Compare working tree with..', Click on it.

enter image description here

  1. Choose the branch you want to compare with, and move to the 'file changes' section:

enter image description here

That's it :)

Samhita answered 14/1 at 17:20 Comment(0)
R
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

Rident answered 17/11, 2020 at 10:10 Comment(1)
May 2022, gitlens installed, still I can't see the "Search & Compare" viewTaxpayer
M
1

You can use Git tree extension

  1. Install Git tree extension in vs code
  2. Click on tree icon in left sidebar
  3. Change the base to target branch screenshot
  4. Click on the file to see diffrence in the tree
Mexico answered 19/1 at 2:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.