How to create patch file using patch and diff
November 23rd, 2006 mysurface Posted in Developer, diff, Misc, patch | Hits: 425538 | 19 Comments »
Okay, this is what I do. I only know the basic. But before doing this, please backup your source code, patch wrongly will screwup your source code.
First, how to create patch file?
Patch file is a readable file that created by diff with -c (context output format). It doesn’t matter and if you wanna know more, man diff. To patch the entire folder of source codes(as usually people do)I do as bellow:
Assume Original source code at folder Tb01, and latest source code at folder Tb02. And there have multiple sub directories at Tb01 and Tb02 too.
diff -crB Tb01 Tb02 > Tb02.patch
-c context, -r recursive (multiple levels dir), -B is to ignore Blank Lines.
I put -B because blank lines is really useless for patching, sometimes I need to manually read the patch file to track the changes, without -B is really headache.
How to patch?
First of all, please do a dry-run before really patch it. Bare in mind, patch will be working very specifically. Let say the version 3 Tb03.patch is use to patch from Tb02, if you apply patch on Tb01, sometimes it will corrupt your source code. So, to make sure it works, do a dry run. Dry-run means a fake-test, do it at the directory of the source code targeted to patch.
Doing dry-run like this:
patch --dry-run -p1 -i Tb02.patch
The success output looks like this:
patching file TbApi.cpp
patching file TbApi.h
patching file TbCard.cpp
...
The failure ouptut looks like this:
patching file TbCard.cpp
Hunk #2 FAILED at 585.
1 out of 2 hunks FAILED -- saving rejects to file TbCard.cpp.rej
patching file TbCard.h
Hunk #1 FAILED at 57.
Hunk #2 FAILED at 77.
Hunk #3 succeeded at 83 with fuzz 1 (offset -21 lines).
....
At last, if the dry-run is giving good result, do this and enjoy the compilation.
patch -p1 -i Tb02.patch
References:
1. Network Theory Ltd Patch Intro
2. How to create and use a patch in Linux







November 24th, 2006 at 1:16 am
[…] Refers to How to create patch file using patch and diff, you can actually read the diff file to compare the different between files. But what if you wanna compare and edit simultaneously manually? […]
March 28th, 2009 at 2:13 am
Useful example, thanks. There are two things I would add – first, before running patch, cd into the new directory (the one to be patched). That’s how the command knows which tree to modify.
diff -crB Tb01 Tb02 > Tb02.patch
cp -r Tb01 Tb03
cd Tb03
patch –dry-run -p1 -i ../Tb02.patch
Second, if there were files in Tb02 that did not exist in Tb01 and you want them included in the patch, give diff the -N option:
diff -crBN Tb01 Tb02 > Tb02.patch
Lastly, I’d note that diff and patch really only work with text files. It can’t deal with jpegs, pdfs, or other binary objects. This can be a bit of a pain for web and GUI developers.
July 16th, 2009 at 2:41 am
thanks for exemple,
mumblyjoe Says: There are two things I would add – first, before running patch, cd into the new directory (the one to be patched)
thanks for adding.
November 16th, 2009 at 2:30 pm
Hi..it is really a valuable information. I would like to know, will the patch command create a directory?
or do we have an option for creating a directory?
May 11th, 2010 at 3:55 pm
The examlpe is very clear and is very helpful.
Thank you!
July 30th, 2010 at 4:35 pm
thank you!very useful!
January 26th, 2011 at 1:36 am
Thanks for useful tutorial…
January 30th, 2011 at 5:15 am
Good stuff really liked this :)
August 9th, 2011 at 5:56 am
great points altogether, you just received emblem reader. What may you recommend in regards to your post that you just made some days ago? Any positive?
October 20th, 2011 at 3:32 pm
good example
April 26th, 2012 at 1:09 am
[…] try to make a patch as introduced here. Say I have two directories pp1(modified version) and pp0(clean version), I make a patch file […]
October 27th, 2012 at 6:55 am
I like:
diff -u
it is nicer :-)
January 1st, 2013 at 4:09 pm
[…] read here can help make one better than what EXE can do. To create your own patch file learn it from here. There is no way M$ can beat gnu/linux/*nix/bsd on matters like this one simple lipstick painting. […]
April 18th, 2013 at 2:02 am
Perfect and simple
Thank you
June 12th, 2014 at 8:42 pm
[…] How to create patch file using patch and diff […]
May 2nd, 2015 at 4:06 pm
Thanks for ones marvelous posting! I truly enjoyed reading it,
you might be a great author.I will make sure to bookmark your
blog and may come back at some point. I want to encourage
you to ultimately continue your great job, have a nice day!
February 3rd, 2016 at 11:45 pm
[…] How to create patch file using patch and diff – by … – Refers to How to create patch file using patch and diff, you can actually read the diff file to compare the difference b… […]
February 3rd, 2016 at 11:52 pm
[…] How to create patch file using patch and diff – by … – Refers to How to create patch file using patch and diff, you can actually read the diff file to compare the difference b… […]
February 19th, 2019 at 11:25 am
Thanks for the step-by-step instructions! This post is very helpful to noobs, like me, who have only rudimentary skills with Linux.