74

I'm trying to understand how to compare branches or commits using VS 2015.

Using other Git programs, I can easily compare versions, but I can't see how it's done in VS.

Can anyone help?

bubbleking
  • 3,329
  • 3
  • 29
  • 49
Rotem B
  • 1,327
  • 1
  • 15
  • 20
  • 3
    Not in visual studio, but you can always do something like `git difftool --dir-diff branchA branchB`. – mkasberg May 05 '16 at 14:38
  • 4
    In Visual Studio, it's pretty easy to view history, view commit details and compare files with previous versions of those files. But I don't see anything that really compares branch-to-branch, except by using a Pull Request. You can run a PR between any two different branches, and that PR will show you the DIFF of the branches. Then you can abandon that PR. Just a thought. – Steve Kennedy May 05 '16 at 17:29
  • 1
    @SteveKennedy, This is good, thanks. And you don't even have to actually create the pull request - just start to. Before you hit the final Create button, there are still files showing at the bottom. – CindyH Oct 18 '18 at 22:05

6 Answers6

36

To compare a specific object (solution, project, source file,...) in Microsoft Visual Studio (using MVS2015):

  1. Locate the object in the Solution Explorer, and bring up the context menu (right-click): select "View History...". This brings up the History window for this object, with all the commits where the object changed (from any branch).
  2. Multi-select the two commits that you want to compare (left-click on the first one, Ctrl-left-click on the second one).
  3. Now bring up the context menu on either of the selected commits (right_click): select "Compare...". This brings up the Diff window for the object in the respective commits (with the differences highlighted in red -lines removed from first commit- or green -lines added in second commit). You can use the scroll bar in the Diff window, or the "Previous Difference" and "Next Difference" button in the ribbon to go between the differences of the object.

I am not sure that there is a way to compare ALL the items in two different commits (I just invoke GitKraken -free for non-commercial purposes- or any other GUI for git on my local repo). Gitkraken is amazingly simple though: select any two commits, and all the differences between those commits are available at your fingertips.

Flandraco
  • 991
  • 10
  • 11
  • 4
    This also works with the whole commit. If you open "Branches" in Team Explorer. You can right a click Branch an then select "View History" the rest is the same as explained above. – Martini Bianco Dec 16 '17 at 21:40
  • 1
    is there a way to compare uncommitted version with any of the previous branch? This allows me to modify the content before I commit locally.. – Sreenath Jul 09 '18 at 04:17
  • 21
    This does not answer the question about comparing Git *branches* inside the Visual Studio IDE. – Jazimov Oct 03 '18 at 16:07
  • As mentioned by @MartiniBianco, the 'View History' shows the branch names marker on different commits. Selecting the specific commits for the respective branches and then it can be compared.This will provide the ability to compare two Git branches inside the Visual Studio IDE. – tyrion Jul 09 '20 at 11:59
30

I just spent a bit of time playing with the current version of VSTS and figured it out (as of October 2016):

  1. Go to Code -> Branches
  2. Click on the commit diff count to the right of the updated date as in this screenshot: vsts_screenshot

This will take you to a page that shows both a commit difference between the two, and a file comparison

peekama
  • 436
  • 5
  • 5
10

As of October 2017, when you right click on a branch under Code->Branches, you will get this menu. Click on compare branches.

enter image description here

rajeemcariazo
  • 2,476
  • 5
  • 36
  • 62
5

In VS-2022, you can go to View->Git Repository:

enter image description here

And then right-click on the branch you want to compare to and select Compare '<current branch>' with '<other branch>'

enter image description here

BSSchwarzkopf
  • 352
  • 2
  • 11
4

If you want to compare two different branches in Visual Studio 2017 or higher, you can do this by using the "Compare Commits" feature while viewing two different commits in the "View History" window for a single branch. The obvious issue though is that one of the branches must contain the head commit of the other in order to be able to do the compare, and most of the time this isn't the case. Fortunately there is an easy way to accomplish this by making a new temporary branch from one, and merging in the other:

 git checkout -b temp-compare-branch branch-1-name --no-track
 git merge branch-2-name

Note if you get merge conflicts, you can just quickly choose one side or the other at random! It doesn't matter how you resolve the conflicts because you don't actually care about the merge commit. You simply need to complete the merge so that the merge commit's parents both reside in the same branch. Once you're done you can "View History" of your new temporary branch, and then control-click the two corresponding commits to select them both, and then right-click and "Compare Commits" to achieve your goal.

Side note: Oftentimes when I have had to do this, it turned out that I was most interested in the changes on one branch since it split from the other branch. In that case I typically find it more useful to look at the compare of the HEAD of each branch with the merge base, which yields the "set of changes on one branch that aren't in the other". To find the merge base you simply use:

git merge-base branch-1-name branch-2-name

The output of that command is a commit ID, and you can compare that commit with the HEAD on each branch separately without even needing to make a temporary branch, if that set of specific changes is what you're actually looking for.

TTT
  • 22,611
  • 8
  • 63
  • 69
  • 2
    +1 The first sentence of this answer is exactly what I needed. I have a main branch and created a feature branch from it to implement a new feature. Next I wanted to see ALL of the changes made in the feature branch so I can review them before merging. By using the "Compare Commits" feature I was able to get a list of all changes in Visual Studio where I can easily click to launch my diff tool in FULL SCREEN mode (Azure DevOps allows comparing branches but the screen space is so tiny and you can't use another diff tool...hence the interest in using Visual Studio). – Eric Mutta Apr 30 '21 at 12:43
2

This can be done easily if you are using Azure DevOps. (I realize this doesn't really answer the question, but I thought others might find this helpful.)

Go to your Repo in Azure DevOps. Go to Branches. When you move your mouse to the right an ellipses will appear.

Click set a compare branch. Now other branches will list how many commits they are 'Behind | Ahead' in relation to this branch.

Click compare branch. You can set any two branches and view the file difference and the commit difference. The direction of the compare matters.

Azure DevOps Branch Comparison Snip

CBFT
  • 273
  • 2
  • 14