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?
Solution 1:
To compare a specific object (solution, project, source file,...) in Microsoft Visual Studio (using MVS2015):
- 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).
- Multi-select the two commits that you want to compare (left-click on the first one, Ctrl-left-click on the second one).
- 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.
Solution 2:
I just spent a bit of time playing with the current version of VSTS and figured it out (as of October 2016):
- Go to Code -> Branches
- Click on the commit diff count to the right of the updated date as in this screenshot:
This will take you to a page that shows both a commit difference between the two, and a file comparison
Solution 3:
As of October 2017, when you right click on a branch under Code->Branches, you will get this menu. Click on compare branches.
Solution 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.
Solution 5:
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