How to Use Meld diff Tool in Linux

Written by: Dwijadas Dey   |   Last updated: March 13, 2024

If you are a developer and often need to compare files and directories then Meld is a cool visual diff and merge tool you may want to have in your system. Meld enables you to review code changes, merge conflicts, and understand patches graphically without any difficulties.

Moreover, Meld is compatible with version-controlled systems including Git, Subversion, Bazaar, and Mercurial. 

Installing Meld on Linux

Install Meld in  Ubuntu/Debian-based system including Raspbian using the following apt command.

$ sudo apt install meld

On a Fedora/Red Hat-based system use dnf to install Meld:

$ sudo dnf install meld

In the macOS system, use Homebrew:

$ brew install meld

Windows users can download the MSI package and install it by following the instructions or using the Chocolately package manager.

Getting Started with Meld

Once you have installed Meld in your system, run it by clicking the executable. For example, in an Ubuntu-based system, you can search Meld in the application menu by pressing the Super + A key.

Open Meld application

Invoke the Meld application and get ready to use its GUI. It is also very much possible to use the Meld from the command line interface. Run man meld to familiarize yourself with all the available options.

$ man meld

Basic Usage of Meld

If you have used the Linux diff command previously then you will appreciate Meld once you get started using it.  Let's run a simple test case where we will try to find the differences between two simple files using both the Linux diff command and Meld.

File 1:  add1.sh

#!/bin/bash
# Calculate the sum of two integers with pre-initialized values
# in a shell script

# Initialize two variables with a number
a=10
b=20

# Calculate sum
sum=$(( $a + $b ))

# Display the result
echo "Sum is: $sum"

File2: add2.sh

#!/bin/bash
# Calculate the sum of two integers with pre-initialized values

# Initialize two variables with a number
a=10
var_b=20

# Calculate sum
sum=$(( $a + $var_b ))

# Display the result
echo "Sum is: $sum"

Run the diff command to find the differences between the above two files.

$ diff add1.sh add2.sh 
3d2
< # in a shell script
7c6
< b=20
---
> var_b=20
10c9
< sum=$(( $a + $b ))
---
> sum=$(( $a + $var_b ))

Let's try to make a difference between the same two files again but by using Meld.

$ meld add1.sh add2.sh

Meld will open the two files side by side in the Windows pane of its GUI.

compare two files using meld

As you can see, the output of the Meld command is much cleaner than the Linux diff command!  It is now uncomplicated to spot the differences between two files in the Meld GUI visually and merge them either way with a single mouse click.

Understanding the Meld Interface

When Meld is invoked without any option from CLI or by clicking the executable, You are presented with three options under the ‘new comparison’ tab.

  • File comparison
  • Directory comparison
  • Version control view
meld interface

Click the appropriate button for file comparison, directory comparison, and even version control view which we will look at a later stage.

To further customize the Meld user interface click Meld->Preferences and choose the options that suit you.

meld preferences

Comparing Files and Directories

File comparison

In Meld GUI, select the two files that you want to compare and click the compare button. Meld opens up two files side by side and also marks the line where there is a difference.

meld compare two files side by side

Click on the arrow button either way to merge the difference and once done you can save the files. 

Directory Comparison

In the Meld UI, click the button ‘Directory comparison’ and then select the folders you want to compare, and finally click the compare button. Meld will list the two directories side by side for comparison. Missing files will be marked as strike-through text.  

meld compare two directories

Viewing and Navigating Differences

The differences for files are shown as the left and right arrows in the Meld UI. Click either of these arrows to merge the differing lines. To view and navigate all the differences between the files use the Up/Down arrow from the toolbar. 

difference between two files in green color

In the case of folder differences, contents from both folders are listed side by side. Choose a file from the Meld UI and click the ‘Compare’ button from the toolbar to view the differences. Meld will open a new tab to list the differences between the two files.

folders listed side by side

Merging Changes with Meld

In the Meld GUI, select a file from the Windows pane and click the ‘Compare’ button from the toolbar to view the differences. In the case of folder differences, Meld will open a new tab to list the differences between two files side by side. 

In the new tab, all the differences between the two files will be marked by an arrow key. To merge the differences, select a difference from the Windows pane and click the ‘Changes’ menu. Merge the files after applying a suitable option from the ‘Changes’ menu.

For missing files, merge them by clicking either the ‘copy to Left’ or ‘copy to Right’ button from the toolbar.

merge with meld

The changes menu in the Meld has a rich set of options to navigate and resolve/merge differences in files quickly.

Integrating Meld with Version Control Systems

Meld can be integrated with version-controlled systems including Git, Subversion, Bazaar, and Mercurial. For this article, we will configure Meld with Git to view and resolve/merge differences from different branches and commits for a Git repository.

Edit the global git config to add the following piece of configuration option from the command line interface.

$ git config --global --edit
...
...

        tool = meld
[difftool]
        prompt = false
[difftool "meld"]
        cmd = meld "$LOCAL" "$REMOTE"
...
...
enable meld with git

Confirm the git settings by listing the options.

$ git config --global --list

Meld is now configured to compare files in git repositories. To compare two commits, run the following meld difftool command.

$ cd your-local-repo
// git difftool <commit id 1> <commit id 2>
$ git difftool a10d7f206b6449e9a98178e392faf084e8a083f6 3412f3ac9fc5ce47b1aef1db015abf004ba52929

Use the following set of commands to view and resolve differences between a file from a remote branch and the one at the local repository.

$ cd your-local-repo
// git difftool branch_name file_name
$ git difftool branch-a custom_install_on_centos.txt

To view the differences between a file from a particular commit with the one from the local repository, run the following git difftool command.

$ cd your-local-repo
// git difftool <commit_hash> <file_name>
$ git difftool a10d7f206b6449e9a98178e392faf084e8a083f6 custom_install_on_centos.txt

Finally, use the following set of commands to view the differences between all files from two different branches of a git repository.

$ cd your-local-repo
$ git checkout branch-a
// git difftool -d <another-branch>
$ git difftool -d branch-b

To Conclude

Meld is a great tool to find and resolve differences between files using its GUI and makes troubleshooting with files a bit easier. Further, Meld’s integration with the version control system enables it to be used as a universal file difference tool in any system. Try Meld by installing it to your system and make the troubleshooting easier at least visually.

About The Author

Dwijadas Dey

Dwijadas Dey

Dwijadas Dey is an open-source software advocate and tech enthusiast. He has been writing blogs about emerging technology for more than 10 years for different cloud service providers and tech sites. He also provides hands-on training and coaching on varied subjects like Kubernetes, Ansible, OpenStack, and Data Streaming.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment