Diff
A process that detects and displays the differences between two texts. It is a foundational technology of version control and code review.
A diff is a process that compares two text files or sets of data and detects and displays the parts that were added, deleted, or changed. Its starting point is the diff command included in the Fifth Edition of Unix in 1974. The idea of finding the longest common subsequence line by line (the sequence of lines that appear in the same order in both files) and showing the lines outside it as deletions or additions is still carried on in today's implementations. Today it plays a core role in version control systems such as Git, SVN, and Mercurial, and it is also widely used in code review and in automating deployments.
There are several kinds of diff algorithms. What Git uses by default is a Myers-style greedy algorithm, which the official documentation also describes as "the basic greedy diff algorithm." A frequent misunderstanding here is that the default algorithm does not guarantee the smallest diff. Git separately provides minimal, which spends extra time on minimizing the diff, and if you need the smallest diff you specify --diff-algorithm=minimal explicitly. Patience diff uses lines that appear only once in one of the files as clues for matching, which displays structural changes to code in a more readable way. Histogram diff extends patience so that it also handles common elements that appear only a few times. The practical criterion for judgment is that "even for the same change, only the way the diff is presented changes, and the content of the changed file is naturally identical with any algorithm." So when you run into a diff that is hard to read, the way to use them is to switch algorithms and compare. To switch permanently, use the diff.algorithm setting.
There are also several kinds of diff output formats. The unified diff format shows added lines with + and deleted lines with -, and includes context lines before and after each changed section. The default context is 3 lines before and after, and you can increase or decrease it with -U<n>. The line @@ -2,7 +2,7 @@ at the start of a hunk means "7 lines starting at line 2 before the change, and 7 lines starting at line 2 after the change," and it serves as a clue that prevents accidents where you misread the line numbers and fix a different place. The diff views you see on pull request screens in GitHub and GitLab are based on this format. Side-by-side diff is a format that shows the before and after versions next to each other on the left and right, and it has the advantage of making changes easy to grasp visually.
In practice, diff is used in a wide range of situations, not only in code review. Typical examples include tracking changes to configuration files, managing versions of documents, and detecting changes to database schemas. In CI/CD pipelines, diff results are also used to narrow down what to test and what to deploy.
Because diff compares line by line, there are several well-known pitfalls. First, simply converting line endings from LF to CRLF turns every line into a difference. For a 3-line file the output is 3 lines deleted and 3 lines added, and it looks like a complete rewrite even though not a single character of the content has changed. Second, a change that only re-indents code also appears as line differences in the same way. These disappear when you add -w (ignore differences in whitespace), so you can use it to isolate meaningful changes when they are buried among whitespace changes. Third, if the newline at the end of a file is lost, the last line becomes a difference with the note \ No newline at end of file. Fourth, a change that only moves code to another place is also displayed as two places, a deletion and an addition. Git has --color-moved, which colors moved code differently, but it is disabled by default. All of these are examples where the "number of lines in the diff" does not represent the size of the change, and this is why, in a review, you should not judge by the number of lines alone.
A concept that is easily confused with diff is the patch. Whereas a diff is "the result of detecting differences," a patch is "a set of instructions for applying those differences." The traditional Unix workflow of generating a diff with the diff command and applying it with the patch command is still used today on the mailing lists of open source projects and in similar places.
From a character counting perspective, diff output contains meta information (line numbers, change markers, file paths, hunk headers, and so on) in addition to the original text, so the character count of diff output is larger than that of the original text. Also, when you want to see changes at the level of words or characters rather than lines, you use word diff. However, as its name suggests, word diff works word by word, and word boundaries depend on whitespace. In English, the change the quick brown fox → the quick brawn fox appears as a replacement of brown with brawn, but because Japanese is not separated by spaces, with the default settings an entire sentence is treated as a single word, and the output differs little from a line-level diff. For diffs of edits to Japanese text, specifying a delimiter regular expression that works character by character (--word-diff-regex=. in Git) makes only the few characters you actually corrected stand out. Even when comparing the same sentence, the difference is that the default shows a replacement of the whole sentence, while the character-level setting shows only the 3 inserted characters as the diff.