Frequently Asked Question

How do diff and patch work together?

diff compares two files and prints the differences. In its traditional output < lines are from the first file and > lines are from the second. diff -u uses the unified format, which shows a few lines of context around each change and marks added lines with + and removed lines with -. Unified diffs are what you see on GitHub, in git log -p, and in mailing-list patches.

patch is the inverse: feed it a diff (usually saved as a .patch file) and the original tree, and it applies the changes line for line. diff -u old new > change.patch produces the patch; patch < change.patch (run in the directory containing the old version) applies it. This was the standard way to exchange source-code changes before Git: maintainers would email unified diffs to mailing lists, and contributors would apply them with patch -p1. Git's internal model is still based on diffs and patches, it just dresses them up.

Further reading and video