The diff command compares two files line by line and prints the differences, and the patch command applies that output to a file to turn the old version into the new one. A typical workflow is diff -u old.txt new.txt > change.patch followed by patch old.txt < change.patch. Together they are the foundation of how changes are shared in many open source projects.
Version control tools such as Git use the same unified format internally, so understanding diff -u makes every code review and pull request easier to read. This guide uses GNU diffutils and GNU patch.
Create two small versions of a configuration file.
This is the default (normal) format. 1c1 means line 1 of the first file was changed into line 1 of the second. Lines starting with < come from the first file and lines starting with > come from the second. The letters are a for added, d for deleted and c for changed.
The exit status is useful in scripts: 0 means the files are identical, 1 means they differ and 2 means an error occurred.
The unified format, requested with -u, is the standard for patches because it shows context around each change.
How to read it:
--- line names the old file and +++ the new one.@@ -1,3 +1,4 @@ hunk header says: starting at line 1 for 3 lines in the old file, and line 1 for 4 lines in the new one.- were removed, lines beginning with + were added, and lines beginning with a space are unchanged context.The timestamps in your output will show the real modification times of your files.
| Option | What it does |
|---|---|
-u | Unified format with 3 lines of context |
-U 5 | Unified format with 5 lines of context |
-y or --side-by-side | Show the two files in two columns |
-i | Ignore case differences |
-w | Ignore all whitespace |
-b | Ignore changes in the amount of whitespace |
-B | Ignore blank-line changes |
-r | Compare directories recursively |
-N | Treat missing files as empty (for new or deleted files) |
--color=auto | Colour the output on a terminal |
For a recursive directory diff meant to be applied later, -ruN is the common combination: recursive, unified, and new files included.
Save the unified diff to a file. Then apply it to the old file.
Now work.conf matches new.conf. You can also let patch read the file names from the patch header by omitting the target.
For patches that cover a directory tree, use the -p option to strip leading path components. A patch created from a/src/main.c and b/src/main.c is applied from the project root with -p1.
Always preview a patch before touching files.
If the preview reports no errors, run it for real. To undo an applied patch, use -R (reverse).
To keep a copy of every file that patch modifies, add -b, which writes the original as file.orig.
When a hunk cannot be applied, patch creates a .rej file with the rejected hunks and continues with the rest. Open the .rej file, apply those changes by hand and then delete it. Fuzz and offset messages such as Hunk #1 succeeded at 12 (offset 2 lines) mean the code moved slightly but the patch still applied.
For code in a repository, git diff produces the same unified format and git apply or git am applies it, with better handling of renames and binary files. Plain diff and patch remain valuable for configuration files, quick comparisons on servers without Git, comparing two directories, and sending a small change by email. Related tools include cmp for byte-level comparison, comm for sorted files and vimdiff for interactive merging.
It means the files are different, not that an error occurred. Status 0 means identical and status 2 means trouble such as a missing file.
Run diff -r dir1 dir2 for a recursive comparison. Add -q to list only which files differ, without the content.
The file you are patching differs from the one the patch was made against. Check that you are using the right base version and the correct -p level, then fix the rejected hunks in the .rej file manually.
Yes. Use -w to ignore all whitespace or -b to ignore changes in the amount of whitespace.
diff -u shows what changed between two files in a compact, readable format, and patch applies that output to bring another copy up to date. Use -r and -N for directories, -p1 to strip path prefixes, --dry-run before applying and -R to undo. These two commands are the base of patch-based workflows and explain what Git shows you every day.