Heads-up
Do not use -p0 unless you understand how patch works.
Diff
Create a deep (recursive) .diff between two directories in the same parent directory. A .diff file is a file that describes all the textual differences.
diff --unified --recursive --no-dereference ORIGINAL/ PATCHED/ > patch.diff
--unified: to format the output into a “unified context diff.”
--recursive: to create a deep .diff.
--no-dereference: not to follow symbolic links.
Although the directory names, ORIGINAL and PATCHED, are included in file paths in the output, patch.diff, they are not important (“won’t be used later on”).
Patch
With the patch.diff file, you can patch any directory of the same hierarchical structure. You don’t need the directories named ORIGINAL and PATCHED anymore.
For example, this command patches the directory_to_apply_the_patch_on/ directory according to the patch.diff.
patch --directory=directory_to_apply_the_patch_on/ --strip=1 < patch.diff
--directory: to set the working directory for patch.
patch assumes that file paths in patch.diff are relative to the working directory. Those paths have preceding PATCHED/, so you need to strip it first unless you have a PATCHED directory to patch in your working directory.
--strip=<N> or -p<N> strips the first N segments (delimited by slashes) from the file paths specified in the patch.diff file. --strip=1 strips the topmost directory PATCHED/ from all the destination file paths, making them relative to directory_to_apply_the_patch_on/ instead.
Vikram Dattu: is it possible to omit those outer directory names? Or is it neccessary that patch should contain new and orig directory names?
Flimm: Is it possible to apply the patch without requiring that directories named orig or new exist?
Yes, and that’s what --strip=1 is for.
For me, I prefer this:
patch --directory=directory_to_apply_the_patch_on/ --unified --strip=1 --posix --force --set-utc --verbose < patch.diff
--unified: to interpret the .diff file as a unified context diff, skipping format guessing.
--posix: to behave the POSIX-compliant way.
--force: not to ask questions on failure.
--set-utc: to update the modified times of patched files.
--verbose: to print what it’s thinking while trying to patch.