Search code examples
gitmeld

Git mergetool with Meld on Windows


In Linux, my favorite merge tool is Meld, and I've had no problems using or configuring it to work with Git. However, in Windows it has been a different story.

First, I installed Meld from a bundle I found here: https://code.google.com/p/meld-installer/

Then, I configured my .gitconfig like so to support Meld as the default mergetool

[merge]                                                      
    tool = meld                                                                         

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

So, when I have a conflict, I do git difftool and Meld does in fact open. However, the paths to the files that Git writes to pass to the diff tool is incorrect. For example, even though Git generates the BASE, LOCAL, and REMOTE files in the repository directory (the location I called git mergetool from), Meld tries to open each of those files in the directory of the executable.

Instead of opening C:\repo\roses.txt.LOCAL.2760.txt, Meld tries to open C:\Program Files (x86)\Meld\meld\roses.txt.LOCAL.2760.txt.

Has anyone ran into this before or know how to configure Git / Meld to work correctly in Windows?


Solution

  • 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. Note: for old versions of Windows, your path is likely "C:\Program Files (x86)\Meld\Meld.exe" instead.

    git config --global merge.tool meld
    git config --global mergetool.meld.path "C:\Program Files\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!). Note: again, for old versions of Windows, your path is likely C:\\Program Files (x86)\\Meld\\Meld.exe instead:

    [merge]
      tool = meld
    [mergetool "meld"]
      path = C:\\Program Files\\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.sstatic.net/sNR7J.png

    (source) enter image description here

    All operating systems: configure meld preferences

    When done installing Meld: open Meld --> (on Windows) click the 3 horizontal lines (settings) in the top-right; (on Linux) click "Meld" at the top --> click "Preferences" --> "Editor" tab --> check the boxes for "Enable text wrapping" and "Show line numbers". See the screenshot below:

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