diff/patch a file
diff originalfile updatedfile > patchfile.patch
patch originalfile -i patchfile.patch -o updatedfile
-u: unified format (keeps your patch as compact as possible)
-c: context format
diff/patch a directory
diff -r -c originaldirectory/ updateddirectory/ > patchfile.patch
cd /path/to/parent/to/originaldirectory
patch -p0 -i patchfile.patch
cd /path/to/originaldirectory
patch -p1 -i patchfile.patch
Bad practice: move into originaldirectory and run the patch command there. (see https://linuxacademy.com/blog/linux/introduction-using-diff-and-patch/)
undo a patch
Reversing an applied patch
cd /path/to/parent/to/originaldirectory
patch -p0 -R -i patchfile.patch
option
patch -b take a backup of the original file before applying the patch command
diff -N -N Treat absent files as empty
p4 perforce
create a patch file from p4
p4 diff -du hello/world/main.cc > ~/patch.diff
patch --dry-run < ~/patch.diff
Validate the Patch without Applying --dry-run
p4 -ztag opened | grep clientFile| awk ' {print $3}'
p4 -ztag opened | grep clientFile| awk ' {print $3}' | sed s+//master_ming_138+.+ | xargs -n1 echo p4 edit
# echo: dry-run
git diff
git diff > patch
patch -p1 < patch
https://stackoverflow.com/questions/3418277/how-to-apply-git-diff-patch-without-git-installed
Latest versions of patch command (2.7, released in September 2012) support most features of the "diff --git" format, including renames and copies, permission changes, and symlink diffs (but not yet binary diffs)
else
Using diff to create a simple patch https://linuxacademy.com/blog/linux/introduction-using-diff-and-patch/
Force a "git stash pop" https://coderwall.com/p/anxp0g/force-a-git-stash-pop https://stackoverflow.com/questions/1360712/git-stash-cannot-apply-to-a-dirty-working-tree-please-stage-your-changes/3733698#3733698
$ git stash show -p | git apply -3 && git stash drop