Git mergetool 与 Windows 上的 Meld

在 Linux 中,我最喜欢的合并工具是 Meld,我在使用或配置它使其与 Git 一起工作时没有遇到任何问题。但在 Windows 系统中,情况就不同了。

首先,我从这里找到的一个包中安装了 Meld: https://code.google.com/p/meld-installer/

然后,像这样配置我的. gitconfig 以支持 Meld 作为默认 mergetool

[merge]
tool = meld


[mergetool "meld"]
path = C:\\Program Files (x86)\\Meld\\meld\\meld.exe
keepBackup = false
trustExitCode = false

所以,当我有一个冲突,我做 git distool 和 Meld 实际上打开。但是,Git 写入要传递给 diff 工具的文件的路径是不正确的。例如,即使 Git 在存储库目录(我从这个位置调用 Git mergetool)中生成 BASE、 LOCAL 和 REMOTE 文件,Meld 也会尝试打开可执行文件目录中的每个文件。

与打开 C: repo roses.txt.LOCAL. 2760.txt 不同,Meld 尝试打开 C: Program Files (x86) Meld Meld roses.txt.LOCAL. 2760.txt。

以前有人遇到过这种情况吗? 或者有人知道如何配置 Git/Meld 在 Windows 中正确工作吗?

78536 次浏览

I had the exact same problem and found I had to brute force my way to get it to work. Here is what I put in my .gitconfig file. (Note my meld executable is in a different location)

[merge]
tool = meld
[mergetool "meld"]
cmd = "/c/Meld/meld/meld.exe $PWD/$LOCAL $PWD/$BASE $PWD/$REMOTE --output=$PWD/$MERGED"

Schuess, be careful for space character in directories!

[merge]
tool = meld
[mergetool "meld"]
prompt = false
keepBackup = false
keepTemporaries = false
path = C:/Program Files (x86)/Meld/Meld.exe
cmd = \"/C/Program Files (x86)/Meld/Meld.exe\" \"$PWD/$LOCAL\" \"$PWD/$BASE\" \"$PWD/$REMOTE\" \"--output=$PWD/$MERGED\"

I found a solution in a bug report on the meld installer, on this page:

https://code.google.com/p/meld-installer/issues/detail?id=11

As far as I understand, the problem is that the meld.exe program (which runs meld through the python interpreter) needlessly sets the command's working directory to that of meld.exe. This causes relative paths to be interpreted incorrectly when passed as command line arguments.

The solution is to replace the provided meld.exe with one generated by compiling the meld.ahk file, using AHK2EXe (AutoHotKey script -> exe). Just download the script furthest down the page, as there have been a few version posted there.

Why do you not use git bash for Windows?

After install meld simply:

git config --global merge.tool meld
git config --global mergetool.meld.path "C:\Program Files (x86)\Meld\Meld.exe" <- path to meld here

Thats all!

I too faced similar issue.Operating system used is Windows 10 and the following changes worked for me.It seems more like path issue

git config --global mergetool.meld.path "/c/Program Files (x86)/Meld/Meld.exe" <- path to meld here

After trying all of the above, setting Meld to run as administrator worked for me.

  1. Right-click Meld.exe
  2. Go to Properties > Compatibility and select the Run this program as an administrator checkbox

The errors I received referenced temp files like c:\windows\temp\meld-*, which were not being created. Elevating Meld's permissions seems to do the trick as it now works with both git difftool and running manually within Meld.

How to use meld as your git difftool in place of git diff

(see also the screenshots of meld below):

1. Windows:

Download and install Git for Windows, which includes a "Git Bash" Linux-like terminal accessible via the right-click menu in any folder in Windows Explorer, once you've installed Git for Windows.

Download and install meld from here: https://meldmerge.org/.

Then, to make meld your git difftool, you can use these two commands, inside the Git for Windows bash terminal, (as Arugin says), using the proper path to Meld.exe:

git config --global merge.tool meld
git config --global mergetool.meld.path "C:\Program Files (x86)\Meld\Meld.exe"

OR you can just edit your C:\Users\YOUR_USER_NAME\.gitconfig file directly and add the following to the end of it (notice the mandatory usage of the double-backslashes [\\] here as the path separator!):

[merge]
tool = meld
[mergetool "meld"]
path = C:\\Program Files (x86)\\Meld\\Meld.exe

Now call git difftool in your Git for Windows bash terminal and Meld will open up as your default difftool viewer! If you don't already know: you can open said terminal in Windows by right-clicking in a folder in Windows Explorer and going to --> "Git Bash" or whatever it's called.

2. Linux:

I might as well put the Linux instructions here too for my own reference in one place if nothing else:

For Linux it's even easier:

# 1. install meld
sudo apt update
sudo apt install meld
# 2. edit your ~/.gitconfig file (gedit GUI editor will open)
gedit ~/.gitconfig

Then add to the bottom of the .gitconfig file:

[diff]
tool = meld

That's it! git difftool now works on Linux Ubuntu!

3. Mac OS

Install Meld on Mac OS: https://superuser.com/questions/360007/how-to-install-meld-with-homebrew-on-mac-osx/1177575#1177575.

Sample screenshots of Meld

https://i.stack.imgur.com/sNR7J.png

(source) enter image description here

Usage of Meld

# 1. See changes you've made since your last commit (do this in place of
# `git diff`)
git difftool


# 2. Calling meld directly to compare two files:
meld path/to/file1.txt path/to/file2.txt

To review someone else's GitHub PR locally on your machine, using meld

NB: the below commands assume their branch is in your same code repo, since you are teammates. If this is not the case, you'll have to slightly modify the commands to check out from their forked repo instead of from your common repo. GitHub will show you the commands they recommend for checking out their branch when looking online at the PR.

  1. The commands only
    git fetch origin someone_elses_branch
    git checkout someone_elses_branch
    git difftool main...HEAD  # 3 dots, NOT 2!
    
  2. Commands with detailed comments
    # 1. Fetch their remote changes to your local machine into your
    # locally-stored,remote-tracking hidden branch named
    # `origin/someone_elses_branch`
    git fetch origin someone_elses_branch
    # 2. Check out this branch locally (this creates the locally-stored branch
    # named `someone_elses_branch` from the locally-stored remote-tracking
    # hidden branch named `origin/someone_elses_branch`)
    git checkout someone_elses_branch
    # 3. Do a difftool comparison (using meld now) to see the changes
    # made on this branch from the point where they last checked out
    # and forked off of `main`. This cmd (using 3 dots) is the equivalent of
    # `git difftool $(git merge-base main HEAD) HEAD`.
    git difftool main...HEAD  # 3 dots, NOT 2!
    

Related:

  1. 3 dots vs 2 dots: What are the differences between double-dot ".." and triple-dot "..." in Git diff commit ranges?
  2. [my answer on my git blametool] Is there git blame gui similar to bzr qannotate?
  3. Download and install meld from here: https://meldmerge.org/
  4. [my answer] How do I make git use the editor of my choice for commits?
  5. [my repo] https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
  6. Install Meld on Mac OS: https://superuser.com/questions/360007/how-to-install-meld-with-homebrew-on-mac-osx/1177575#1177575

None of the answers worked for me. I ended up with this in the .gitconfig-file:

[merge]
tool = meld
[mergetool "meld"]
cmd = 'C:/Program Files (x86)/Meld/Meld.exe' $LOCAL $BASE $REMOTE --output=$MERGED
[mergetool]
prompt = false

After a git merge mybranch ending with conflicts, you simply type git mergetool and meld opens. After a save, you have to commit in git and the conflicts are resolved.

For some reason, this only worked with Meld 3.18.x, Meld 3.20.x gives me an error.

For some reason, in Windows 10 the PATH environmental variable could not be set properly during the installation, so a strange exception is raised saying that it is not able to find some .dll that are under "C:\Program Files (x86)/Meld/bin" directory.

A workaround for me was, execute in git bash:

export PATH=$PATH:"/C/Program Files (x86)/Meld/lib"

Or add to windows PATH

C:\Program Files (x86)/Meld/bin