git visual diff between branches

GitDiff

Git Problem Overview


This answer is great for seeing a visual diff between two files that are checked into git: https://stackoverflow.com/questions/255202/how-do-i-view-git-diff-output-with-a-visual-diff-program/949242#949242

However, I'd like to see a visual diff between two branches. So far, my best bet seems to be:

git diff --name-status master dev

which isn't very informative and not very visual.

Is there anything better out there?

Git Solutions


Solution 1 - Git

Use git diff with a range.

git diff branch1..branch2

This will compare the tips of each branch.

If you really want some GUI software, you can try something like SourceTree which supports Mac OS X and Windows.

Solution 2 - Git

To see a visual diff of all differences between two branches I like to merge the two branches - WITHOUT committing the merge - and then use git gui or git Extensions to get an overview of the differences.

Git command line for merging without commiting:

git checkout branchA
git merge --no-commit --no-ff branchB


Then when done, you can undo the merge with

git merge --abort
(h/t to @jcugat's for the comment)

Solution 3 - Git

In case you are using Intellij Idea IDE, you could just use the compare option in the branch.

enter image description here

Solution 4 - Git

You can also do this easily with gitk.

> gitk branch1 branch2

First click on the tip of branch1. Now right-click on the tip of branch2 and select Diff this->selected.

Solution 5 - Git

For those of you on Windows using TortoiseGit, you can get a somewhat visual comparison through this rather obscure feature:

  1. Navigate to the folder you want to compare
  2. Hold down shift and right-click it
  3. Go to TortoiseGit -> Browse Reference
  4. Use ctrl to select two branches to compare
  5. Right-click your selection and click "Compare selected refs"

Source: http://wikgren.fi/compare-diff-branches-in-tortoise-git-or-how-to-preview-changes-before-doing-a-merge/

Solution 6 - Git

If you are using OSX or Windows 7+, Atlassian SourceTree works very well for this. It is free.

You can see staged changes in a side-by-side diff setup, and you easily compare local with remote and any other two branches. When multiple files are selected, the diff shows up as below:

enter image description here

Assuming you have checked out a feature branch and you want to see the diff against 'master', right-click on the 'master' branch and select "Diff against current"

Unfortunately, it doesn't seem as if it will be available on *nix distributions anytime soon.

Solution 7 - Git

Try "difftool" (assuming you have diff tools setup) - see https://www.kernel.org/pub/software/scm/git/docs/git-difftool.html

I find name status good for the summary but difftool will iterate the changes (and the -d option gives you the directory view), e.g.

$ git difftool their-branch my-branch

Viewing: 'file1.txt'
Launch 'bc3' [Y/n]:
...

Or as @rsilva4 mentioned with -d and default to your current branch it is just - e.g. compare to master:

$  git difftool -d master..

...and yes - there are many variations - https://www.kernel.org/pub/software/scm/git/docs/git-reset.html

Solution 8 - Git

In GitExtensions you can select both branches in revision grid with Ctrl pressed. Then you can see files that differ between those branches. When you select a file you will see diff for it.

Taken from here

Or you can right-click on any revision and choose Compare > Compare Branch...

right-click menu

Then you get to choose which branch to compare to:

select branch

And you end up with a Diff window listing the files that are different between the two branches, and for each file it shows a diff view.

diff view

Solution 9 - Git

If you're using github you can use the website for this:

github.com/url/to/your/repo/compare/SHA_of_tip_of_one_branch...SHA_of_tip_of_another_branch

That will show you a compare of the two.

Solution 10 - Git

UPDATE

Mac: I now use SourceTree. Thoroughly recommended. I especially like the way you can stage / unstage hunks.

Linux: I've had success with:

  • smartgit
  • GitKraken
  • meld

E.g. to install smartgit on Ubuntu:


This does the job:

git-diffall with a GUI diff tool like meld. See point 5 here:

http://rubyglazed.com/post/15772234418/git-ify-your-command-line

There's a nice post about git and meld here: http://nathanhoad.net/how-to-meld-for-git-diffs-in-ubuntu-hardy

Solution 11 - Git

If you use Eclipse you can visually compare your current branch on the workspace with another tag/branch:

Eclipse workspace compare

Solution 12 - Git

You can use the free P4Merge from Perforce to do this as well:

http://www.perforce.com/product/components/perforce-visual-merge-and-diff-tools

enter image description here

Details on integrating it with Git can be found here and here

but a quick summary from the above links is:

  • Put the following bits in your ~/.gitconfig, and then you can do $ git mergetool and $ git difftool to use p4merge
  • Note that $ git diff will still just use the default inline diff viewer :) (tested with git version 1.8.2)

Changes for .gitconfig

[merge]
  keepBackup = false
    tool = p4merge
[mergetool "p4merge"]
    cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge "\"$PWD/$BASE\"" "\"$PWD/$REMOTE\"" "\"$PWD/$LOCAL\"" "\"$PWD/$MERGED\""
    keepTemporaries = false
    trustExitCode = false
    keepBackup = false
[diff]
    tool = p4merge
[difftool "p4merge"]
    cmd = /Applications/p4merge.app/Contents/Resources/launchp4merge "\"$REMOTE\"" "\"$LOCAL\""

Solution 13 - Git

If you use the excellent WebStorm editor, you can compare with any branch you'd like:

Webstorm git compare

Solution 14 - Git

Have a look at git show-branch

There's a lot you can do with core git functionality. It might be good to specify what you'd like to include in your visual diff. Most answers focus on line-by-line diffs of commits, where your example focuses on names of files affected in a given commit.

One visual that seems not to be addressed is how to see the commits that branches contain (whether in common or uniquely).

For this visual, I'm a big fan of git show-branch; it breaks out a well organized table of commits per branch back to the common ancestor.

Solution 15 - Git

Here is how to see the visual diff between whole commits, as opposed to single files, in Visual Studio (tested in VS 2017). Unfortunately, it works only for commits within one branch: In the "Team Explorer", choose the "Branches" view, right-click on the repo, and choose "View history" as in the following image.

enter image description here

Then the history of the current branch appears in the main area. (Where branches that ended as earlier commits on the current branch are marked by labels.) Now select a couple of commits with Ctrl-Left, then right click and select "Compare Commits..." from the pop-up menu.

For more on comparing branches in the Microsoft world, see this stackoverflow question: https://stackoverflow.com/questions/37052450/differences-between-git-branches-using-visual-studio.

Solution 16 - Git

You can also use vscode to compare branches using extension CodeLense, this is already answered in this SO: https://stackoverflow.com/q/42112526/2781524

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSnowcrashView Question on Stackoverflow
Solution 1 - GitalexView Answer on Stackoverflow
Solution 2 - GitTormod HystadView Answer on Stackoverflow
Solution 3 - GitKamal ReddyView Answer on Stackoverflow
Solution 4 - GitMartinView Answer on Stackoverflow
Solution 5 - GitOversearchView Answer on Stackoverflow
Solution 6 - GitJordan ParkerView Answer on Stackoverflow
Solution 7 - GitPaul KohlerView Answer on Stackoverflow
Solution 8 - GitMahmood DehghanView Answer on Stackoverflow
Solution 9 - GitArronView Answer on Stackoverflow
Solution 10 - GitSnowcrashView Answer on Stackoverflow
Solution 11 - GitAlessandro DionisiView Answer on Stackoverflow
Solution 12 - GitBrad ParksView Answer on Stackoverflow
Solution 13 - GitNicoView Answer on Stackoverflow
Solution 14 - GitKay VView Answer on Stackoverflow
Solution 15 - GitCarsten FührmannView Answer on Stackoverflow
Solution 16 - GitPehejeView Answer on Stackoverflow