Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between git branches using Visual Studio

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?

like image 351
Rotem B Avatar asked May 05 '16 13:05

Rotem B


People also ask

How do I see differences between two branches in Git Visual Studio?

To compare your currently checked out branch with other branches using Visual Studio, you can utilize the branch picker hosted in the status bar and the Git changes tool window to choose any local or remote branch to compare with. Right click the branch you are targeting and select Compare with Current Branch.

What is difference between Git branch and Git branch?

git branch creates the branch but you remain in the current branch that you have checked out. git checkout -b creates a branch and checks it out. Let's rather say: "git branch creates the branch but you remain in the current branch FROM WHICH you have checked out."


4 Answers

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.

like image 58
Flandraco Avatar answered Oct 22 '22 09:10

Flandraco


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

like image 20
peekama Avatar answered Oct 22 '22 08:10

peekama


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

like image 26
rajeemcariazo Avatar answered Oct 22 '22 09:10

rajeemcariazo


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.

like image 4
TTT Avatar answered Oct 22 '22 07:10

TTT