patch
Text processing
Usage
patch -p1 < [file.patch]Applies a diff to real files; -p decides how many leading path components to drop, and a git diff needs -p1 because of its a/ and b/ prefixes.
Common flags
| Flag | Meaning |
|---|---|
| -pN | Strip N leading path components; git diffs need -p1 |
| -R | Apply the patch backwards to undo it |
| --dry-run | Test whether it applies and change nothing |
| -i | Read the patch from a file instead of standard input |
| -b | Keep a backup of every file it changes |
Examples
patch -p1 < fix.patchApplies a git-style patch from the repo root.
patch -p1 -R < fix.patchUndoes it again.
patch -p1 --dry-run < fix.patchChecks it applies cleanly first.
These were built to be piped together. Three joined commands are usually shorter than one that does everything.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does patch do?
Applies a diff to real files; -p decides how many leading path components to drop, and a git diff needs -p1 because of its a/ and b/ prefixes.
Q. How do I type it?
patch -p1 < [file.patch] — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
5 are listed here; the full set is in man patch. This command sits under Text processing.
Related commands
man page: man patch