Apply a diff/patch file to update source files.
medium risk
patch applies changes described in a diff file to the original files. The standard workflow is: generate a patch with diff -u, review it, then apply with patch. Used for distributing fixes without sending full files.
| Flag | Meaning |
|---|---|
-p --strip | Strip N leading path components from filenames |
-R --reverse | Reverse: undo a previously applied patch |
--dry-run | Preview what patch would do without applying |
-b --backup | Backup original files before patching |
patch -p1 < fix.patchApply patch (strip 1 path component, standard for git patches) · explain & risk-check →patch --dry-run -p1 < fix.patchPreview patch application · explain & risk-check →patch -R -p1 < fix.patchReverse (undo) a patch · explain & risk-check →git diff HEAD~1 HEAD > change.patch && patch -p1 < change.patchBackport a git commit as a patch · explain & risk-check →Always use `patch --dry-run` first to verify a patch applies cleanly before committing — especially critical for patches from external sources against your specific version.
Reference text is hand-written and may be incomplete — check man patch for your version. Test destructive commands on a non-production system first.