DEV Community

abbazs
abbazs

Posted on

How to use Meld as a difftool for Git?

Meld is a graphical tool that lets you compare and merge files or directories. You can use it as a difftool for Git to visually inspect the changes in your Git repositories. Here are the steps to set up and use Meld as a difftool for Git:

  1. Check if you have Meld installed on your system by running this command in a terminal or command prompt:
   meld --version
Enter fullscreen mode Exit fullscreen mode

If you see the version information, you are good to go. Skip to step 3. If not, follow the next step to install Meld.

  1. Install Meld on your system depending on your operating system:
  • For Ubuntu or Debian-based systems, run:

     sudo apt-get install meld
    
  • For Fedora or CentOS-based systems, run:

     sudo dnf install meld
    
  • For macOS systems with Homebrew, run:

     brew install meld
    
  • For Windows systems, download the installer from https://meldmerge.org/ and follow the instructions.

  1. Create or edit your global .gitconfig file by running this command in a terminal or command prompt:
   git config --global --edit
Enter fullscreen mode Exit fullscreen mode

This will open the file in your default text editor.

  1. Add these lines to the file to tell Git to use Meld as the difftool:
   [diff]
       tool = meld
   [difftool]
       prompt = false
   [difftool "meld"]
       cmd = meld "$LOCAL" "$REMOTE"
Enter fullscreen mode Exit fullscreen mode

These lines set Meld as the difftool and provide the command for launching it with the right file parameters. The prompt = false line stops Git from asking you every time if you want to use Meld or not. Don't worry, this won't affect the normal git diff command which will still work as usual.

  1. Save and close the file.

  2. Check that the file is updated correctly by running this command:

   git config --global --list
Enter fullscreen mode Exit fullscreen mode

You should see the configuration options you added for Meld in the list.

  1. You are ready to use Meld as the difftool by running git difftool in your Git repositories. For example, to compare two commits, run:

    git difftool <commit> <commit>
    

    Git will open Meld and show you the differences between the commits for easy comparison.

You can use git difftool just like git diff. For example, to compare a file in different branches or commits, run:

```
git difftool <BRANCH_NAME> file_name
git difftool <COMMIT_HASH> file_name
git difftool <COMMIT_HASH_1> <COMMIT_HASH_2> file_name
```
Enter fullscreen mode Exit fullscreen mode

Meld will display the diff in a graphical interface with two panes. The order of the panes depends on the order of $LOCAL and $REMOTE in the cmd line. If you want to swap them, just change the order like this:

cmd = meld "$REMOTE" "$LOCAL"
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
ellabebop profile image
ellabebop • Edited

Thanks for the article. I recently switched from xxdiff to meld as my git-diff tool. I'm mostly pleased but have been disappointed in how meld labels the files in the title bar and the chooser button above each file's content. No directory information is shown, and one or both files is a cryptically-named temporary, e.g., hVNj6T_foobar, and sometimes collapsed with an ellipsis if too long, even when there's plenty of space. When comparing lots of files between two commits, it's hard to know where I am within the two trees or even what files are being compared. It'd sure be nice if there was a way to show something more informative in the UI. Using --label once or twice on the meld command line helps.