How does git diff –patience differ from a regular git diff?

To understand the difference between git diff --patience and a regular git diff, it’s important to first understand what git diff does.

At a high level, git diff is a Git command used to show changes between commits, commit and working tree, etc. It shows the changes made in terms of lines added and/or removed.

The algorithm that the regular git diff uses to identify differences between two versions of a file is called the “Myers algorithm”. This algorithm is a type of ‘greedy’ algorithm, where the main goal is efficiency. It tries to minimize the number of lines in the diff output, but it doesn’t necessarily provide the most human-friendly output. This algorithm is the default because it works relatively fast for large files and it performs well in most common cases.

Now, git diff --patience uses a different diff algorithm known as the “Patience Diff algorithm”. The idea behind the Patience Diff algorithm is to produce diff outputs that are more understandable for humans. It is specifically designed to yield better results for certain types of code changes.

The key difference is in the way the two algorithms handle cases where lines are moved or copied. The Patience Diff algorithm is designed to deal with this more effectively. It looks for unique lines in the code and uses these as anchor points, creating blocks of code around these anchor points. These blocks can then be compared for moved or copied lines, which leads to more accurate and readable diffs.

Here is an example of how this can make a difference:

Imagine you have two versions of a file with a list of items. In the second version, you simply moved some lines around.

Regular git diff may show this as lines being removed and then other lines being added, even though what actually happened is that lines were just moved.

On the other hand, git diff --patience will more likely correctly show that lines were moved around, without showing extraneous add/remove operations. It’s more likely to generate a minimal diff in these circumstances.

One potential downside to the Patience Diff algorithm is that it can be slower than the regular algorithm, especially on large files, because it is more complex and takes more computations. However, for most files, this difference in performance is negligible.

In conclusion, git diff --patience and regular git diff are two tools that can be used depending on your needs. If you’re interested in the most minimal and fastest diff, or you’re dealing with very large files, the regular git diff may be the best choice. But if you want a diff that’s potentially easier to understand, especially in cases where lines have been moved around, then git diff --patience could be the tool for you.