Andrew Mercer
on this page

Tutorial: How to create a patch with diff (ShellHacks).

# Create: diff -u ORIGINAL MODIFIED
diff -u lib/databases/mysql.orig lib/databases/mysql > mysql.patch

# Apply to a file: patch TARGET PATCHFILE
patch lib/databases/mysql mysql.patch

# Or feed the patch on stdin and let the patch's own file names decide
patch -p1 < mysql.patch                 # strip one leading path component

patch --dry-run -p1 < mysql.patch       # check that it applies cleanly
patch -R -p1 < mysql.patch              # reverse (undo) a patch

Error: "Only garbage was found in the patch input"

The arguments are in the wrong order. The file to be patched comes first, and the patch file second:

patch mysql.patch lib/databases/mysql     # WRONG: treats mysql.patch as the file to patch
patch lib/databases/mysql mysql.patch     # right

For whole trees, diff -ruN old/ new/ > changes.patch and patch -p1 < changes.patch from inside the tree. Git users can use git diff / git apply. See diff.