Creating a Patch File
diff -Naur old-dir-or-file new-dir-or-file > patchfile.patch
How to Apply a Patch
There are two common ways to apply a patch:
# Method 1
cat new-patch | patch -p0
# Method 2
patch -p0 < new-patch
Understanding the -p (strip) Parameter
The -p parameter specifies how many leading directory components to strip from paths in the patch file.
For example, if a patch file begins with:
--- old/modules/network JAN 26 12:11:36 2002
+++ new/modules/network SEP 20 20:07:38 2003
-p0: Start from the current directory and look for the full pathold/modules/network-p1: Strip the first directory level (oldornew), then look formodules/networkfrom the current directory
Patch File Structure
- Lines beginning with
---and+++indicate the files to be patched. - A single patch file can contain multiple patches — each patch is a separate
---/+++block. - Each block (hunk) represents a section to be modified, typically surrounded by unchanged context lines for location reference.
- Hunks begin with
@@and end at the start of another hunk or a new patch header. - Hunk indentation:
+at the start of a line: this line is to be added-at the start of a line: this line is to be deleted
Comments & Feedback