Text Diff - Understanding Unified Diff Format

The Unified Diff Format, also known as Unidiff, is a common way of displaying differences between files. This format is most commonly seen in the output of tools like diff or git diff. A unified diff presents differences in a useful way that makes it easy to see what changes need to be made to go from one version of a file to another.

A unified diff is composed of one or more diff hunks. Here's an example of a diff hunk:

@@ -1,3 +1,2 @@
This is an unchanged line
-This is a removed line
+This is an added line
This is an unchanged line

The @@ -1,3 +1,2 @@ part is the hunk header. The -1,3 part means that the hunk starts from line 1 and shows 3 lines from the original file. The +1,2 part means that the hunk starts from line 1 and shows 2 lines from the new file. Essentially, it indicates the change in line numbers between the two versions.

Lines that are removed from the original file are preceded by a -, while lines added in the new file are preceded by a +. Lines that haven't changed are often shown to give context to the changes.


Consider two simple text files:

File1.txt Apple
File2.txt Apple

The unified diff between these two files would look like this:

@@ -1,5 +1,4 @@

This diff is saying that in order to transform File1.txt into File2.txt, you need to remove the "Banana" line, add the "Blueberry" line, and remove the "Elderberry" line. The other lines ("Apple", "Cherry", and "Date") remain the same in both files.