不喜欢 diff 么？试试 Meld 吧
Meld diff 比较

> 
> Meld 是一个可视化 diff 工具，它可让你轻松比较和合并文件、目录、Git 仓库等的更改。
> 
> 
> 


![](/data/attachment/album/202004/03/122428dkagz1qtgh9f2g10.jpg)


Meld 是我处理代码和数据文件的基本工具之一。它是一个图形化的 diff 工具，因此，如果你曾经使用过 `diff` 命令并难以理解输出，那么 [Meld](https://meldmerge.org/) 可以为你提供帮助。


这是该项目网站的精彩描述：



> 
> “Meld 是面向开发人员的可视化 diff 和合并工具。Meld 可以帮助你比较文件、目录和版本控制的项目。它提供文件和目录的双向和三向比较，并支持许多流行的版本控制系统。”
> 
> 
> “Meld 可以帮助你检查代码更改并了解补丁。它甚至可以帮助你弄清你一直在避免的合并中发生了什么。”
> 
> 
> 


你可以使用以下命令在 Debian/Ubuntu 系统（包括 Raspbian）上安装 Meld：



```
$ sudo apt install meld
```

在 Fedora 或类似产品上：



```
$ sudo dnf install meld
```

Meld 是跨平台的，它有一个使用 [Chocolately](https://opensource.com/article/20/3/chocolatey) 包管理器的 [Windows 安装包](https://chocolatey.org/packages/meld)。尽管它在 macOS 上不受官方支持，但有[可用于 Mac 的版本](https://yousseb.github.io/meld/)，你可以使用 Homebrew 安装：



```
$ brew cask install meld
```

有关[其他系统](https://meldmerge.org/)，请参见 Meld 的主页。


### Meld 对比 diff 命令


如果你有两个相似的文件（也许一个是另一个的修改版本），并想要查看它们之间的更改，那么可以在终端中运行 `diff` 命令查看它们的区别：


![diff output](/data/attachment/album/202004/03/122502d0a5m1hx5m1xa50d.png "diff output")


此例显示了 `conway1.py` 和 `conway2.py` 之间的区别。表明我：


* 删除了[释伴](https://en.wikipedia.org/wiki/Shebang_(Unix))和第二行
* 从类声明中删除了 `(object)`
* 为类添加了 docstring
* 在方法中交换了 `alive` 和 `neighbours == 2` 的顺序


这是使用 `meld` 命令的相同例子。你可以在命令行中运行以下命令进行相同的比较：



```
$ meld conway1.py conway2.py
```

![Meld output](/data/attachment/album/202004/03/122515bssq8ug8o88jgo8s.png "Meld output")


Meld 更清晰！


你可以轻松查看并单击箭头（左右都行）合并文件之间的更改。你甚至可以实时编辑文件（在输入时，Meld 可以用作具有实时比较功能的简单文本编辑器）—只是要记得在关闭窗口之前保存。


你甚至可以比较和编辑三个不同的文件：


![Comparing three files in Meld](/data/attachment/album/202004/03/122529msab4qh6q6htp6s8.png "Comparing three files in Meld")


### Meld 的 Git 感知


希望你正在使用 [Git](https://opensource.com/resources/what-is-git) 之类的版本控制系统。如果是这样，那么你的比较就不是在两个不同文件之间进行，而是要查找当前文件与 Git 历史文件之间的差异。Meld 理解这一点，因此，如果你运行 `meld conway.py`（`conway.py` 在 Git 中），它将显示自上次 Git 提交以来所做的更改：


![Comparing Git files in Meld](/data/attachment/album/202004/03/122546fqk6b776jkhy6w1b.png "Comparing Git files in Meld")


你可以看到当前版本（右侧）和仓库版本（左侧）之间的更改。你可以看到，自上次提交以来，我删除了一个方法，并添加了一个参数和一个循环。


如果你运行 `meld .`，你将看到当前目录（如果位于仓库的根目录，就是整个仓库）中的所有更改：


![Meld . output](/data/attachment/album/202004/03/122553b3vbmgfgb31y9g3n.png "Meld . output")


你会看到一个文件被修改了，另一个文件未加入版本控制（这意味着它对 Git 是新的，因此在比较之前，我需要 `git add` 添加该文件），以及许多其他未修改的文件。顶部的图标提供了各种显示选项。


你还可以比较两个目录，这有时很方便：


![Comparing directories in Meld](/data/attachment/album/202004/03/122601jqorbnbn2o82o2rq.png "Comparing directories in Meld")


### 结论


即使是普通用户也会觉得 diff 的比较难以理解。我发现 Meld 提供的可视化在找出文件之间的更改方面有很大的不同。最重要的是，Meld 有一些有用的版本控制认知，可以帮助你在不考虑太多内容的情况下对 Git 提交进行比较。快来试试 Meld，并轻松解决问题。




---


*本文最初发表在 Ben Nuttall 的 [Tooling blog](https://tooling.bennuttall.com/meld/) 上，并经允许重新使用。*




---


via: <https://opensource.com/article/20/3/meld>


作者：[Ben Nuttall](https://opensource.com/users/bennuttall) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Meld is one of my essential tools for working with code and data files. It's a graphical diff tool, so if you've ever used the **diff** command and struggled to make sense of the output, [Meld](https://meldmerge.org/) is here to help.

Here is a brilliant description from the project's website:

"Meld is a visual diff and merge tool targeted at developers. Meld helps you compare files, directories, and version controlled projects. It provides two- and three-way comparison of both files and directories, and has support for many popular version control systems.

"Meld helps you review code changes and understand patches. It might even help you to figure out what is going on in that merge you keep avoiding."


You can install Meld on Debian/Ubuntu systems (including Raspbian) with:

`$ sudo apt install meld`


On Fedora or similar, it's:

`$ sudo dnf install meld`


Meld is cross-platform—there's a [Windows install](https://chocolatey.org/packages/meld) using the [Chocolately](https://opensource.com/article/20/3/chocolatey) package manager. While it's not officially supported on macOS, there are [builds available for Mac](https://yousseb.github.io/meld/), and you can install it on Homebrew with:

`$ brew cask install meld`


See Meld's homepage for [additional options](https://meldmerge.org/).

## Meld vs. the diff command

If you have two similar files (perhaps one is a modified version of the other) and want to see the changes between them, you could run the **diff** command to see their differences in the terminal:

![diff output diff output](https://opensource.com/sites/default/files/uploads/diff-output.png)

This example shows the differences between **conway1.py** and **conway2.py**. It's showing that I:

- Removed the
[shebang](https://en.wikipedia.org/wiki/Shebang_(Unix))and second line - Removed
**(object)**from the class declaration - Added a docstring to the class
- Swapped the order of
**alive**and**neighbours == 2**in a method

Here's the same example using the **meld** command. You can run the same comparison from the command line with:

`$ meld conway1.py conway2.py`



![Meld output Meld output](https://opensource.com/sites/default/files/uploads/meld-output.png)

Much clearer!

You can easily see changes and merge changes between files by clicking the arrows (they work both ways). You can even edit the files live (Meld doubles up as a simple text editor with live comparisons as you type)—just be sure to save before you close the window.

You can even compare and edit three different files:

![Comparing three files in Meld Comparing three files in Meld](https://opensource.com/sites/default/files/uploads/meld-3-files.png)

## Meld's Git-awareness

Hopefully, you're using a version control system like [Git](https://opensource.com/resources/what-is-git). If so, your comparison isn't between two different files but to find differences between the current working file and the one Git knows. Meld understands this, so if you run **meld conway.py**, where **conway.py** is known by Git, it'll show you any changes made since the last Git commit:

![Comparing Git files in Meld Comparing Git files in Meld](https://opensource.com/sites/default/files/uploads/meld-git.png)

You can see changes made in the current version (on the right) and the repository version (on the left). You can see I deleted a method and added a parameter and a loop since the last commit.

If you run **meld .**, you'll see all the changes in the current directory (or the whole repository, if you're in its root):

![Meld . output Meld . output](https://opensource.com/sites/default/files/uploads/meld-directory-changes.png)

You can see a single file is modified, another file is unversioned (meaning it's new to Git, so I need to **git add** the file before comparing it), and lots of other unmodified files. Various display options are provided by icons along the top.

You can also compare two directories, which is sometimes handy:

![Comparing directories in Meld Comparing directories in Meld](https://opensource.com/sites/default/files/uploads/meld-directory-compare.png)

## Conclusion


Even regular users can find comparisons with diff difficult to decipher. I find the visualizations Meld provides make a big difference in troubleshooting what's changed between files. On top of that, Meld comes with some helpful awareness of version control and helps you compare across Git commits without thinking much about it. Give Meld a go, and make troubleshooting a little easier on the eyes.

*This was originally published on Ben Nuttall's Tooling blog and is reused with permission.*

## 6 Comments