patch: apply a diff file to an original
patch [OPTION]... [ORIGFILE [PATCHFILE]]
Options you will use
-p N, --strip=N- Remove N leading path parts from the names in the diff: -p1 turns a/src/x.c into src/x.c (what git diffs need). Without -p only the base name is used.
-i FILE, --input=FILE- Read the patch from FILE instead of standard input.
--dry-run- Report what would happen ("checking file ...") and change nothing.
-R, --reverse- Apply the patch backwards: undo a patch that was applied.
-N, --forward- Skip a patch that looks already applied instead of asking.
-b, --backup- Keep the original as FILE.orig.
-F N, --fuzz=N- How many context lines a hunk may ignore at each end to still fit (default 2).
-d DIR, --directory=DIR- Change to DIR first.
-s, --silent- Print only errors.
Examples
$ patch -p1 --dry-run < fix.diffcheck first
$ patch -p1 < fix.diffapply a diff made with diff -ru a b or git
$ patch -R -p1 < fix.diffundo it
Gotchas
- "Hunk #1 succeeded at 12 (offset 3 lines)." = the lines moved but the hunk still fit; patch then keeps the original as FILE.orig. "with fuzz 1" = it fitted only by ignoring a context line: check that hunk by hand.
- "Hunk #2 FAILED" = it did not fit anywhere; it is saved in FILE.rej and the exit status is 1. Apply it by hand, then delete the .rej.
- Exit status: 0 everything applied, 1 some hunks failed, 2 trouble.
Try patch in a real terminal Free, in your browser - a real Ubuntu terminal to try it in, with missions that check your work.