patch

Apply a diff/patch file to update source files.

medium risk

interactive breakdown — edit the command and press Explain
$ 
⚠ Risks & cautions
What it does

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.

Common flags
FlagMeaning
-p --stripStrip N leading path components from filenames
-R --reverseReverse: undo a previously applied patch
--dry-runPreview what patch would do without applying
-b --backupBackup original files before patching
Examples
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 →
Pro tip

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.

Related
diff git cherry-pick sed
Open the full explainer →More in Text Processing

Reference text is hand-written and may be incomplete — check man patch for your version. Test destructive commands on a non-production system first.