Instead of creating new versions or forking a packages simply just noting what should be changed in the original released code can be written computer readable. The computer than can apply it automatically. This is what a patch is.

Creating a patch

Patches are just text files showing the output of diff

diff <original_file> <modified_file> will show the difference in plain text in the console, but information is missing to locate the lines. Therefore there is the unified format:

diff --unified <original_file> <modified_file> prints addition information to understand better what and where the differences are.

Alternatively of --unified --context could be used to get an other format.

Finally the patch need to go into a file diff --unified <original_file> <modified_file> > <name>.patch

Instead of single files also directories can be passed to diff however diff --recursive must be added that also subdirectories are compared. This brings in the complexity of the file paths. One way is to have two subdirectories , one containing the original the other then modified files. Then:

diff --unified --recursive <original_dir> <modified_dir> > <name>.patch

Applying a patch

patch < <name>.patch will do the job when having path and original file in the same directory.

However this is also not always the case. Going into the directory where the file to patch is and adding the path to the patch works. patch < <path>/<name>.patch

Finally when not being in the directory of the file to be patched there is the -p<n> command line option that shortens the path of the file to be patched inside the patch file so the original file is found. This option is very often used in scripts that build the source.


Do not create the patch in the directory where the file to be patched is, since then no path appears in the patch file and an automated script running in the top directory will not find the file since it has no indication of the subdirectories names and structure. The the -p<n> command line option can just remove path names but not add.

The necessary number to the -p<n> command line option tells how much of the path needs to be shortened. patch -p0 < <name>.patch

Now diff <file1><file2> , diff -s <file1><file2> or diff --report-identical-files <file1><file2> will confirm the success.

The above works just if <file1> has a different name than <file2> but normally they have the same name and are therefore in two different directories. In this case diff needs to have also the path to those two files.

Linurs startpage