How To Create File Patches
Single File
This is how to build a patch file that you then apply on a target system to modify one of its files.
Get original copy
cp /pathtofile/myfile.conf /pathtofile/myfile.conf.orig
Edit the file manually
Using any editor, manually make changes to the file /pathtofile/myfile.conf as desired.
Capture the difference in a patch
diff -u /pathtofile/myfile.conf.orig /pathtofile/myfile.conf >myfile.patch
Try the patch
Undo the manual changes you made
patch -R -p0 <myfile.patch
Redo your changes
patch -p0 <myfile.patch
Recursive Directory
In this example capture all the changes we make to a tree of pelican-themes into a single patch file.
Duplicate
We start by duplicating the complete tree.
cp -r ${HOME}/pelican-themes ${HOME}/pelican-themes.orig
TIP
I have a tendency keep my orig copy and reuse it a lot as I modify things. To guard against inadvertantly changing something within the orig version without noticing and thereby undermining all subsequent diffs may make, I like to boiler plate the .orig files a bit by write protecting them. This is not necessary but it has served me well.
chmod -w --recursive pelican-themes.orig/
Create Patch File
Now we make all the changes we want within the pelican-themes directory. Once that is done we create the patch file incorporationg all changes.
cd ${HOME}/pelican-themes
diff -Naru ../pelican-themes.orig . > ../pelican-themes.patch
Note:
-N --new-file treat absent files as empty-a --text treat all files as text-r --recursive recursively compare subdirectories-u -U --unified[=NUM] Output NUM lines of unified context (default 3)
Undo Changes
cd ${HOME}/pelican-themes
patch -Rs -p0 < ${HOME}/pelican-themes.patch
Note:
Remember that directories created by patch must be removed manually-R --reverse Assume the patch was created with old and new files swapped-s --silent be quiet unless error occurs-pN Strip smallest filepath prefix containing N slashes. 0 means full path.
Apply Patch Changes
cd ${HOME}/pelican-themes
patch -s -p0 < ${HOME}/pelican-themes.patch