Patch is a command that is used to apply patch files to the files like source code, configuration. Patch files holds the difference between original file and new file. In order to get the difference or patch we use diff
tool.
Software is consist of a bunch of source code. The source code is developed by developers and changes in time. Getting whole new file for each change is not a practical and fast way. So distributing only changes is the best way. The changes applied to the old file and than new file or patched file is compiled for new version of software.
Syntax
patch [options] [originalfile [patchfile]] patch -pnum <patchfile
Help
$ patch --help

Create Patch File
Now we will create patch file in this step but we need some simple source code with two different version. We call the source code file name as myapp.c
.
myapp_old.c
#include <stdio.h> void main(){ printf("Hi poftut"); }
myapp.c
#include <stdio.h> void main(){ printf("Hi poftut"); printf("This is new line as a patch"); }
Now we will create a patch file named myapp.patch
.
$ diff -u myapp_old.c myapp.c > myapp.patch

We can print myapp.patch
file with following command
$ cat myapp.patch
Apply Patch File
Now we have a patch file and we assume we have transferred this patch file to the system which holds the old source code which is named myapp_old.patch
. We will simply apply this patch file. Here is what the patch file contains
- the name of the patched file
- the different content
$ patch < myapp.patch

Take Backup Before Applying Patch
One of the useful feature is taking backups before applying patches. We will use -b
option to take backup. In our example we will patch our source code file with myapp.patch
.
$ patch -b < myapp.patch

The backup name will be the same as source code file just adding the .orig
extension. So backup file name will be myapp.c.orig
Set Backup File Version
While taking backup there may be all ready an backup file. So we need to save multiple backup files without overwriting. There is -V
option which will set the versioning mechanism of the original file. In this example we will use numbered
versioning.
$ patch -b -V numbered < myapp.patch

As we can see from screenshot the new backup file is named as number like myapp.c.~1~
Validate Patch File Without Applying or Dry run
We may want to only validate or see the result of the patching. There is a option for this feature. We will use --dry-run
option to only emulate patching process but not change any file really.
$ patch --dry-run < myapp.patch
Reverse Patch
Some times we may need to patch in reverse order. So the apply process will be in reverse. We can use -R
parameter for this operation. In the example we will patch myapp_old.c
rather than myapp.c
$ patch -R myapp_old.c < myapp.patch

As we can see that new changes are reverted back.
Thanks for the writetup to help me to demystify the patching process. The hands on tutorial definitely helped me.
The ability to reverse the patch was most helpful!
very well and detailed explanation of the patch utility. Was able to simulate and practice it for better understanding, thanks for your efforts !