190

I know that I can view the difference between HEAD and current state with meld .. But how can I view the differences between branches, for example master and devel with meld?

At the moment I do the following steps:

  1. Rename folder of working copy
    For example mv /projectA /projectA_master)
  2. Clone the project again
    git clone url
  3. Switch to devel branch
    cd projectA && git -b devel origin/devel
  4. View differences with meld
    meld /projectA_Master projectA

Isn't there an easier way to get the same result in meld? I only need it to review the changes and not primarily for merging.

Marten Bauer
  • 3,099
  • 5
  • 22
  • 18
  • Does this answer your question? [Setting up and using Meld as your git difftool and mergetool](https://stackoverflow.com/questions/34119866/setting-up-and-using-meld-as-your-git-difftool-and-mergetool) – evan.bovie Jun 05 '20 at 16:30

8 Answers8

381

Short & sweet:

git config --global diff.tool meld

This configures Git to use meld as the diff tool. (You don't need to specify the command line arguments, support for meld is built into Git.)

Then, if you want a graphical diff instead of a textual one, you simply invoke git difftool instead of git diff (they both take the same arguments). In your case:

git difftool master..devel

Update: If you don't want the one-file-at-a-time diff, but instead want to use meld's "subdirectory" view with all the changes between the two branches, note the -d or --dir-diff option for git difftool. For example, when I'm on branch XYZ and I want to see what is different between this and branch ABC, I run this:

git difftool -d ABC
ScumCoder
  • 690
  • 1
  • 8
  • 20
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • 3
    that's not what I'm looking for. It shows me the differences file by file. I archived that with a script diff.py and 'git diff master..devel' before. I want to see all differences and the directory tree as 'meld folderA/ folderB/' does. – Marten Bauer Jan 07 '10 at 14:58
  • Marten, that's the way git work. It only track file, so you can only see difference file by file. In git, you cannot commit an empty dir alone. Any particular reason you want to display diff between dir? – Donny Kurnia Jan 22 '10 at 23:58
  • @DonnyKurnia: It took me a bit to figure out what OP is trying to do: Meld has a separate UI for viewing all changes in a directory. You can filter viewing files based on if they're same, changed, new. OP wants to use that UI to show the changes. (This lets you see a list of all changes and pick the ones you want to diff.) So it's not a comparison between directories, but a comparison between commits but viewed as a whole. – idbrii Aug 05 '12 at 19:55
  • 22
    @MartenBauer I think this is what you want: git difftool --dir-diff master devel – Stéphane Dec 22 '13 at 05:27
  • 3
    Can this be done so that the current branch isn't in a tmp folder and therefore allow edits? – zkent Mar 03 '14 at 17:02
  • we can use it with `git gui` at "tools/add" like `git difftool -d $REVISION`! – Aquarius Power Jun 20 '14 at 06:03
  • This work fine ! But if the `-d` option do not work as excepted, try to update your meld version. Take a look to : http://stackoverflow.com/questions/28833983/using-meld-as-difftool-for-git if you have this problem. – Benjamin Mar 10 '15 at 10:38
  • Awesome, thanks! This was always the default for me with `Hg` & `DiffMerge`. Drove me nuts when I moved to `Git`. – Justin May 08 '15 at 17:12
115

Starting with git v1.7.11, you can use git difftool --dir-diff to perform a directory diff. Which works quite well with meld wihout https://github.com/wmanley/git-meld scripts.

Configure git

git config --global diff.tool meld

Use it

git difftool -d topic             // -d is --dir-diff
git difftool -d master..topic

For macOS

brew cask install meld
git config --global difftool.meld.cmd 'open -W -a Meld --args \"$LOCAL\" \"$PWD/$REMOTE\"'
git config --global difftool.meld.trustExitCode true
GutenYe
  • 3,227
  • 2
  • 25
  • 21
  • 3
    I think this is really what the OP wanted. Note the -g option to use the guidiff tool and the -d option to use a --dir-diff. This is good for doing code reviews. Nit: the difftool.prompt option is not required when specifying -d, at least for Git 1.8. – Michael Percy Jul 22 '13 at 17:56
  • 5
    Can this be done so that the current branch isn't in a tmp folder and therefore allow edits? – zkent Mar 03 '14 at 17:03
  • 2
    I'd appreciate an answer to @zkent question as well... :( – tavlima Mar 13 '14 at 20:50
  • 3
    @zkent @tavlima : This command, already, allows edits on your current version. Even if you see a tmp floder into meld, if you save - `Ctrl+s` - the right part, your file is modified. – Benjamin Oct 02 '15 at 12:20
  • i setup exactly as you described but its just simply does nothing, not even Meld opened. when i open Meld manually it works. – To Kra Jan 09 '17 at 13:32
  • This didn't work for me on macOS. See [my answer for a more comprehensive solution](https://stackoverflow.com/a/62201614/764945). – evan.bovie Jun 04 '20 at 18:23
60

I also found this issue annoying so I've made git meld which allows a more comfortable way of diffing arbitrary commits against the working tree or the staging area. You can find it at https://github.com/wmanley/git-meld . It's a bit like Mark's script but works for comparing any arbitrary commit or the staging area or the working directory against any of the others. If one of the things you are comparing against is the working tree then that is read-write also so you don't lose your changes.

Will Manley
  • 2,340
  • 22
  • 17
  • 1
    Excellent tool, Will. Thanks! Thoroughly recommended ... now if only it worked on merges as well. – Dipstick Nov 18 '11 at 15:00
  • TYVM for a great tool - (reminder to self to add ! to the alias) – kfmfe04 Jan 27 '12 at 17:21
  • 29
    Quoting Will, from hit github repository: "NOTE: git-meld is obsolete since git difftool learnt the --dir-diff option in git 1.7.11." – oluc May 05 '13 at 17:26
21

It is important to say that using git difftool -d you can still edit your working files in Meld and save them. In order to achieve that you need to compare some branch to your current working tree, for example:

git difftool -d branchname

Meld will be showing that both left and right directories are located in /tmp. However, files in the right directory are actually symbolic links to your files in the current working directory (does not apply to Windows). So you can edit them right in Meld and when you save them your changes will be saved in your working dir.

Yet more interesting option is comparison of current working dir with stash. You can do that by simply typing:

git difftool -d stash

Then you can transfer some changes from stash (left window) to your current working copy (right window), without using git stash pop/apply and avoiding bothersome conflict resolution which may be induced by this commands.

I think that it can significantly boost up workflow with stashes. You can gradually transfer changes from stash to working copy and commit them one by one, introducing some another changes if you want.

Piotr Jurkiewicz
  • 1,653
  • 21
  • 25
  • Piotr, this is exactly what I've been trying to do, but in my case (on CentOS), no symlinks are being created. Is there any required config setup, or a min version of meld that supports this? – wrjohns May 22 '15 at 17:12
  • I think that Git is responsible for symlinks creation, not Meld. Check Git's manual for difftool command. Maybe you should update it to a newer version? – Piotr Jurkiewicz May 22 '15 at 20:35
  • 2
    copy new files from branch to working dir does not work :( – pykiss Jul 22 '16 at 11:07
6

Although it seems from the other answers as if there's not a way to do this directly in the git repository at the moment, it's easy (thanks to the answer to another question :)) to write a script that will extract the trees of two commits to temporary directories and run meld on them, removing both directories when meld exits:

http://gist.github.com/498628

Of course, you'll lose any changes made via meld, but it's quite nice for a quick overview of the differences, I think.

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
5

I think a easy way for doing this is using git reset --soft:

Goal: compare differences between branch_a and branch_b with meld

git checkout branch_a
git checkout -b do_diff
git reset --soft branch_b
meld .
realtime
  • 395
  • 1
  • 4
  • 11
4

For Meld on macOS, add this to your ~/.gitconfig as recommended by the maintainer of the macOS application, yousseb:

[diff]
  tool = meld
[difftool]
  prompt = false
[difftool "meld"]
  trustExitCode = true
  cmd = open -W -a Meld --args \"$LOCAL\" \"$REMOTE\"
[merge]
  tool = meld
[mergetool]
  prompt = false
[mergetool "meld"]
  trustExitCode = true
  cmd = open -W -a Meld --args --auto-merge \"$LOCAL\" \"$BASE\" \"$REMOTE\" --output=\"$MERGED\"

You can omit the merge configs if you would like.

@GutenYe's answer didn't work out for me due to automatic escaping and/or something with zsh.

evan.bovie
  • 270
  • 2
  • 13
0

In git V1.7.9 you can compare two commits without the commandline:

You must configure in 'git gui' edit options, global: "Use merge tool: meld".

Start gitk, select a commit, right click another commit > "diff this --> selected". Under 'patch' right click a file > "external diff".

meld will start and display the still selected, first commit on the right side.

Stefan Forster
  • 405
  • 4
  • 7