A diff (short for difference) is a representation of what changed between two versions of a text file. Every version control system, every code review tool, and every merge conflict resolution workflow is built on diff algorithms. Understanding how to read a diff and using a good diff tool makes code review, debugging, and auditing dramatically faster.
How Diff Algorithms Work
The most widely used diff algorithm is the Myers algorithm, developed in 1986. It finds the shortest edit script that transforms one text into another. An edit script describes the minimum number of line deletions and insertions needed to go from the original to the modified version.
The output of a diff is typically displayed with lines that were removed marked in red and lines that were added marked in green. Lines that are unchanged are shown in grey or not shown at all, depending on the display mode.
Inline vs Side-by-Side Diff
A unified diff shows the two versions interleaved in a single column, with context lines around each changed section. This is the format git diff uses.
A side-by-side diff shows the original version in the left column and the modified version in the right column. Changed lines are aligned horizontally so you can see the before and after at the same time. This format is easier to read for large blocks of changes.
Using the DevHexLab Code Diff Tool
Open the tool at /tools/text/code-diff. Paste the original version into the left panel and the modified version into the right panel. The tool highlights additions and deletions. You can switch between side-by-side and unified view.
The Code Diff tool supports syntax highlighting for common languages, making it easier to read the changed lines in context.
Frequently Asked Questions
What is the difference between a text diff and a semantic diff?
A text diff compares lines of text character by character. A semantic diff understands the language and compares logical units like functions, methods, and declarations. Text diffs are universal; semantic diffs are language-specific.
Can I use a diff to check if two files are identical?
Yes. If there are no highlighted changes, the files are identical. A diff that shows no differences is confirmation of an exact match.
Use the Code Diff tool any time you need to verify what changed between two versions.