215

This answer is great for seeing a visual diff between two files that are checked into git: How do I view 'git diff' output with a visual diff program?

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?

Community
  • 1
  • 1
Snowcrash
  • 80,579
  • 89
  • 266
  • 376
  • I've added an answer below including a visual that was previously unaddressed here: how to see which commits branches contain (whether in common or uniquely). 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. – Kay V Apr 23 '18 at 13:57

16 Answers16

233

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.

Stéphane
  • 1,476
  • 3
  • 12
  • 12
alex
  • 479,566
  • 201
  • 878
  • 984
  • 32
    It's not visual doe. ;) – marines Jan 02 '14 at 10:44
  • 40
    In Linux systems I recommend installing `Meld`, then setting it as the default `difftool` with `git config --global diff.tool meld` and finally launch it instead of plain `diff` with `git difftool branch1..branch2`. The result is much nicer looking. – Gabriel Jan 06 '14 at 18:26
  • @marines I agree - very handy but it's not visual. See my answer below re `meld`. – Snowcrash Mar 14 '14 at 09:48
  • 16
    using [Meld](http://meldmerge.org/) visual difftool without git config: `git difftool -t meld branch1..branch2` – teichert Jun 14 '16 at 16:15
  • note this [relevant question about the meaningful contrast between `branch1..branch2` and `branch1...branch2`](http://stackoverflow.com/questions/462974/what-are-the-differences-between-double-dot-and-triple-dot-in-git-com) – teichert Jun 14 '16 at 16:29
  • 4
    For readers: As of my post, every answer here gives a way to do what the person asked for (a diff in a GUI) except for this answer. – G Huxley Mar 01 '17 at 20:53
  • 3
    @GHuxley where does the OP ask for a GUI solution? – alex Mar 02 '17 at 08:46
  • 2
    @alex They asked for a "visual diff" which is the exact terminology for a graphical (even ASCII-art) visual diff display – G Huxley May 29 '17 at 23:19
  • @GHuxley Exact terminology as defined by whom? Anyway I added a link to a GUI solution as well. – alex May 30 '17 at 07:26
  • 2
    I have to agree with @GHuxley -- he asked for a visual diff, not a command-line diff. – Cobra Jun 08 '17 at 13:54
  • @alex `git diff branch1..branch2` [is just a synonym](https://git-scm.com/docs/git-diff#Documentation/git-diff.txt-emgitdiffemltoptionsgtltcommitgtltcommitgt--ltpathgt82308203) for `git diff branch1 branch2`. The `..` does make a difference though when using it in `git log` which you linked. – olopopo Feb 04 '21 at 16:32
112

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)

boardrider
  • 5,882
  • 7
  • 49
  • 86
Tormod Hystad
  • 2,345
  • 1
  • 19
  • 15
  • 12
    Don't underestimate the relevance of this approach if you like using your IDE (or other not-always-easy-to-integrate-with-Git GUI difftool)! This option is awesome for me, since I use Intellij and vastly prefer being able to spin through modified files in the IDE, in all its syntax-highlighted, error-highlighted, dead-code-highlighted, editable-in-place glory. I use this approach for all my code reviews, and I have left the abysmal pull request comparision tool in GitHub far behind. I can even make edits as I go, and when done I just create a new branch with "-code-review" appended and commit! – kghastie Feb 25 '14 at 17:51
  • 2
    It works as long as there are no merge conflicts and fails when there are any merge conflicts. – Naga Kiran May 01 '14 at 18:09
  • 1
    I like that approach a lot! Also, at least for me in `git gui` it shows the conflicts as unstaged, the non-conflicts as staged, so you get a good distinction even. Helps a lot more for me than the top-rated answer :D – DJGummikuh Apr 19 '16 at 09:55
64

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

enter image description here

Kamal Reddy
  • 2,610
  • 5
  • 28
  • 36
  • are there any configuration/options/settings to modify this comparison? (like --ignore-space-at-eol for git diff) – Superole Dec 09 '14 at 13:16
60

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.

Martin
  • 721
  • 5
  • 8
35

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/

Oversearch
  • 494
  • 5
  • 6
25

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.

adamwong246
  • 795
  • 9
  • 15
Jordan Parker
  • 1,208
  • 1
  • 16
  • 25
  • 3
    It may be 'free' but it certainly requires some time to read their registration Terms of Use: https://www.atlassian.com/legal/customer-agreement I have so far liked the tool, but this may be pushing me off. – akauppi Dec 05 '14 at 15:35
  • @akauppi Which parts put you off? I thought all those agreements were pretty much the same – alex Feb 01 '16 at 12:17
  • That was over a year ago. Yes, agreements may be similar but it was the process that SourceTree was pushing me through. It was weird. Bygones, though. I won't have the details for you. Sorry – akauppi Feb 04 '16 at 12:42
  • Here is a SourceTree diff question: https://stackoverflow.com/q/30177189/470749 – Ryan Dec 28 '20 at 18:19
18

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

Paul Kohler
  • 2,684
  • 18
  • 31
  • 3
    Thank you, this was useful. Adding the -d option will make things even better: `git difftool -d their-abc my-abc` – rsilva4 Apr 24 '14 at 13:24
  • Since explainshell.com [doesn't seem to be able to parse the -d option](https://github.com/idank/explainshell/issues/179) at the moment, here's what the manpage says: `-d --dir-diff: Copy the modified files to a temporary location and perform a directory diff on them. This mode never prompts before launching the diff tool.` – waldyrious Mar 31 '17 at 16:53
11

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

Rory
  • 40,559
  • 52
  • 175
  • 261
Mahmood Dehghan
  • 7,761
  • 5
  • 54
  • 71
  • I've got 2 branches selected in my revision grid, but I'm not seeing any comparison commands in the menu at the top or when I do a right click. – Eric Nov 22 '16 at 19:22
  • 1
    I found it in the headings of the bottom pane. There are Commit, File tree, and Diff. – Eric Nov 22 '16 at 20:00
8

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.

Arron
  • 767
  • 8
  • 11
7

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

Snowcrash
  • 80,579
  • 89
  • 266
  • 376
5

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

Eclipse workspace compare

Alessandro Dionisi
  • 2,494
  • 4
  • 33
  • 37
  • Personally I find the Eclipse support for diffing sub-par, but if that's all you have then it is OK. – ysap Apr 21 '17 at 23:17
4

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\""
Community
  • 1
  • 1
Brad Parks
  • 66,836
  • 64
  • 257
  • 336
2

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

Webstorm git compare

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
Nico
  • 4,248
  • 1
  • 20
  • 19
  • Actually I just noticed IntelliJ IDEA (presumably also WebStorm) has the ability to "Highlight Non-Picked Commits", which makes it very clear which commits exist in branch but not another. – arthurakay May 23 '17 at 19:19
  • Yeah, I just used intellij to selectively roll off loads of changes made across a repo by "autoformat on save". Was very easy... Had to find the "Compare before with local" option to do it though – Tom Jan 05 '18 at 04:55
2

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. - to try it on a repo with multiple branches with divergences, just type git show-branch and check the output - for a writeup with examples, see Compare Commits Between Git Branches

Kay V
  • 3,738
  • 2
  • 20
  • 20
1

You can also use vscode to compare branches using extension CodeLense, this is already answered in this SO: How to compare different branches on Visual studio code

Peheje
  • 12,542
  • 1
  • 21
  • 30
0

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: Differences between git branches using Visual Studio.

Carsten Führmann
  • 3,119
  • 4
  • 26
  • 24