Home·Terminal commands

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

FlagMeaning
-pNStrip N leading path components; git diffs need -p1
-RApply the patch backwards to undo it
--dry-runTest whether it applies and change nothing
-iRead the patch from a file instead of standard input
-bKeep a backup of every file it changes

Examples

patch -p1 < fix.patch

Applies a git-style patch from the repo root.

patch -p1 -R < fix.patch

Undoes it again.

patch -p1 --dry-run < fix.patch

Checks 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