Luke Oliff.

TIL: Using diff and patch for Incremental File Changes

·TIL·1 min read·Luke Oliff

When you need to share a change with someone who does not use git, diff and patch are your friends.

diff -u original.js updated.js > changes.patch

The diff output shows what changed, with context lines around each hunk. Send the .patch file to someone else.

patch < changes.patch

They run patch and the file updates to match your version. No merge conflicts, no branch, no remote repository.

patch -p1 < changes.patch

The -p1 flag strips the leading directory from paths, which matters when the diff was created from a different directory structure.

Can I reverse a patch?

Yes. patch -R reverses the changes. Useful when someone applies the wrong patch version.

Does diff work on directories?

diff -r compares two directories recursively. diff -r -u old/ new/ shows every file change.