Linux Command Reference ยท Viewing and Searching Files
Linux patch command
Reference for the Linux patch command: apply unified diffs to files and source trees, strip path prefixes with -p, test with --dry-run and reverse patches.
patch reads a description of changes (usually produced by diff -u) and applies it to files. A patch is a small text file, so a change can be mailed, reviewed, stored in version control and applied later, instead of shipping whole copies of the changed files.
patch is tolerant: it matches hunks by their context lines, not only by line numbers, so a patch usually still applies if the target changed elsewhere. When a hunk cannot be placed, patch saves it in a .rej file and tells you.
patch [OPTION]... [ORIGFILE [PATCHFILE]] patch -p1 < change.patch
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
patch [-pNUM] [-R] [-N] [-b] [--dry-run] [-i PATCHFILE] [-d DIR] [FILE]
| Option | Description |
|---|---|
-pN |
Strip N leading path components from file names in the patch. -p1 turns a/src/x.c into src/x.c. |
-i FILE |
Read the patch from FILE instead of standard input. |
-R |
Reverse: undo a patch that was already applied. |
-N |
Ignore patches that look already applied (or reversed). |
--dry-run |
Show what would happen without changing any file. |
-b |
Keep a backup of each changed file as name.orig. |
-d DIR |
Change to DIR before doing anything. |
-F N, --fuzz=N |
Allow up to N lines of context to differ (default 2). |
-l |
Ignore differences in white space when matching. |
-E |
Remove files that become empty. |
--no-backup-if-mismatch |
Do not leave .orig files when a hunk needed fuzz or an offset. |
Returns: 0 when every hunk applied, 1 when some hunks failed (written to .rej files), 2 for serious trouble.
Usage notes
- Choosing
-p. Look at the file names in the patch headers. If they start witha/andb/(as Git produces), use-p1from the project root. If the paths are already relative to your folder, omit-p. A wrong value makespatchask which file to patch, or fail withcan't find file to patch. - Always try
--dry-runfirst on a patch from someone else. It reports hunks that would fail without touching the tree. - Failed hunks. Look for
file.rejnext to the file. It contains the hunks that did not apply, in diff format, so you can merge them by hand. - Offsets and fuzz. Messages like
Hunk #1 succeeded at 12 (offset 3 lines)are normal.fuzzmeans context lines were ignored to find a match; check the result with extra care. - Order matters. Apply a series of patches in the order they were made; each one assumes the result of the previous one.
- Git patches.
patch -p1understands the textual part ofgit diffoutput. Git itself hasgit apply, which is stricter and can also handle renames and binary data. - Line endings. A patch made against LF files may fail on CRLF files; convert line endings or use
--binarycarefully. - Not a security boundary. A patch can modify any path it names, so inspect patches from untrusted sources before applying them.
Related
diff,cmp,comm- Lessons: diff, comm and patch, Git status and diff