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?
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.
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."
To compare a specific object (solution, project, source file,...) in Microsoft Visual Studio (using MVS2015):
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.
I just spent a bit of time playing with the current version of VSTS and figured it out (as of October 2016):
This will take you to a page that shows both a commit difference between the two, and a file comparison
As of October 2017, when you right click on a branch under Code->Branches, you will get this menu. Click on compare branches.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With