How to Set Up Meld as Difftool and Mergetool for Git

John Wachira Feb 02, 2024
  1. Configure Meld as Default Git Difftool
  2. Configure Meld as Default Git Mergetool
How to Set Up Meld as Difftool and Mergetool for Git

This article will discuss configuring Meld as Git’s default diff and merge tools. Meld is an awesome GUI diff program that makes it easier for us to inspect file changes and merge results.

The first step is always downloading and installing the program. Once you are done, follow our lead.

Configure Meld as Default Git Difftool

To configure Git to use Meld as the difftool, we will need to make changes to our .gitconfig file. We will add the lines below to our .gitconfig file.

[diff]
    tool = meld
[difftool]
    prompt = false
[difftool "meld"]
    cmd = meld "$LOCAL" "$REMOTE"

The order is determined by order of the $LOCAL and $REMOTE arguments. In our case, the $LOCAL, our original file, will be on the left and the $REMOTE is the modified file on the right.

If you want the other way around, use this instead.

cmd = meld "$REMOTE" "$LOCAL"

The prompt = false part instructs Git not to ask for confirmation for the launch tool, which it does by default.

We can run the git difftool command as the git diff command.

$ git difftool <BranchName> file_name

Configure Meld as Default Git Mergetool

Meld also makes it easier to resolve conflicts during merging thanks to their GUI mergetool. Again, we will change our .gitconfig file to configure Meld as the mergetool for Git.

Add the lines below to your .gitconfig file.

[merge]
    tool = meld
[mergetool "meld"]
    # Choose one of these 2 lines
    cmd = meld "$LOCAL" "$MERGED" "$REMOTE" --output "$MERGED"
    cmd = meld "$LOCAL" "$BASE" "$REMOTE" --output "$MERGED"

When we have merge conflicts, we can use Meld as our mergetool by running the command below.

$ git mergetool

Let’s take a brief look at the parameters above.

  1. $LOCAL - This is the file in our checked-out branch.
  2. $REMOTE - This is the file in the branch we are attempting to merge.
  3. $MERGED - This is the file with the merge conflicts.
  4. $BASE - This is the original file when the branch containing $REMOTE is made.

$LOCAL and $REMOTE will be on the left and right, depending on your order in the cmd, and you can either have $BASE or $MERGED in the middle. Meld allows for a 3-way view, and we should edit the middle file to resolve the conflicts.

After editing, we can close the program, and Git will update the file automatically. At this point, we are ready to commit the changes and finish merging.

Alternatively, we can run some commands on the Bash to update our .gitconfig file.

For Windows users, run these commands.

$ git config --global diff.tool meld
$ git config --global difftool.meld.path "C:\Program Files (x86)\Meld\Meld.exe"
$ git config --global difftool.prompt false

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

Ensure you feed in the correct path to your MELD.exe file.

Linux users can use the commands below to update their .gitconfig file.

$ git config --global diff.tool meld
$ git config --global difftool.meld.path "/usr/bin/meld"
$ git config --global difftool.prompt false

$ git config --global merge.tool meld
$ git config --global mergetool.meld.path "/usr/bin/meld"
$ git config --global mergetool.prompt false

Update the code above with the correct installation path on your machine.

In a nutshell, Git’s original diff and merge interfaces are unappealing. However, we can configure Git to use Meld as the default GUI difftool and mergetool.

All you need to do is update your .gitconfig file by following the above steps.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Diff