Schematischer Unified Diff einer Codedatei: links die alte Fassung, rechts die neue, in der Mitte ein Block mit Kopfzeile @@ -14,6 +14,7 @@, darunter rot markierte Zeilen mit Minuszeichen, grün markierte Zeilen mit Pluszeichen und graue unveränderte Kontextzeilen.

Diff

A diff is a representation of the differences between two versions of a file, usually marked line by line with plus and minus signs. Programmers use diffs to review changes, and AI assistants also often output their code suggestions in this form.

When you work on a text, there eventually come to be two versions of it: the old one and the new one. A diff shows only what has changed between these two versions. The name comes from the English word “difference”. Typically, a minus sign precedes each removed line, and a plus sign precedes each newly added line. Everything that has stayed the same is either left out or shown only as a gray frame for context. This way, in a file with 2,000 lines, you can see at a glance that exactly three of them have changed.

Why programmers won’t release anything without a diff

Software is rarely written by a single person alone. In a team, everyone submits their changes, and others review them beforehand. This review is called a code review. Without a diff, you’d have to lay two complete files side by side and search through them yourself. With a diff, the review takes minutes instead of hours.

The second reason is debugging. If a program crashes today but still ran last week, the bug is almost always in one of the changes made in between. The diffs from recent days then become the list of all suspects. You work through them until the cause is found.

Third, diffs are compact. An entire program can be hundreds of megabytes in size, while a single change to it may only be a few kilobytes. That’s why version control systems don’t store every version in full, but mainly the differences. This saves an enormous amount of space.

How a diff is calculated and read

A diff is not written by hand but calculated by a program. It compares the two versions and searches for the longest sequence of lines that occurs identically in both. Anything outside this common sequence counts as a change. This approach usually delivers the shortest meaningful explanation of how the old version becomes the new one.

The common output format is called the Unified Diff. It consists of blocks, referred to in technical jargon as hunks. Each block begins with a line like “@@ -14,6 +14,7 @@”. This means: starting at line 14, there were previously 6 lines, and afterward there are 7. Below that are the affected lines, preceded by a minus, a plus, or a space. The lines with a space are unchanged and serve only for orientation.

A diff can also be applied. You give a file the list of changes, and a tool builds the new version from it. In this role, the diff is also called a patch. This only works if the surrounding context still matches. If someone else has since rewritten the same lines, the patch can no longer find its place. This is called a conflict, and you have to decide yourself which version applies.

Diffs in AI assistants and on GitHub

Anyone who looks at software online constantly sees diffs, even if they aren’t called that. On platforms like GitHub or GitLab, every submitted change is displayed as a green-and-red comparison. Green is new, red is removed. Wikipedia also shows exactly this principle when comparing versions.

In the world of AI, diffs have become the standard format for code suggestions. A coding assistant doesn’t rewrite the entire file but delivers only the changed lines as a diff. This has two advantages: the human immediately sees what the assistant wants to touch, and the response stays short. Short responses are directly cheaper with AI models, since payment is per piece of output text.

A typical misconception is to mistake a diff for the change itself. It is only a description of it, and one of several possible ones. If you move a paragraph, one tool might recognize this as “moved”, another as a deletion plus an insertion. Both are correct, but differ in readability. This is exactly what modern diff tools are working on: not just being correct, but being understandable.

Subscribe free. Unsubscribe the second it sucks.

High-signal news across AI, business, UX, and tech. Every morning.