| English | 日本語 |

Tokyo Linux Users Group

Author: Josh Glover

Creating Patches with diff

In a shell, change to the directory where the file you wish to make a patch for is located. Copy the original file (before your edits) to the same directory (you did make a backup copy of it before you edited it, right?). Now, type in the shell:

diff -uN original_file new_file >patch.original_file

where original_file is the filename of the original file and new_file is the filename of the new file (the one containing your edits).

This technique also works for creating a patch for a whole directory tree (e.g. creating a Linux kernel patch). Just add the -r flag to your diff command:

diff -uNr original_dir new_dir >patch.original_dir

Here is a concrete example of creating a patch against the vanilla Linux kernel's source tree:

cd /usr/src
diff -uNr linux-2.4.19 linux-2.4.19-my_version \
  >patch.linux-2.4.19-my_version

That is all there is to creating patches with diff. Move on to #Applying Patches with Patch to see how to apply them with patch.

Applying Patches with Patch

Copy the patch that you generated in Step 2 to the directory containing the file to which you want to apply the patch. Now, type in your shell:

patch <patch_file

where patch_file is the filename of the patch file. Note that the filename of the file to be patched must be the same as it was in #Creating Patches with diff.

If you want to apply a patch to a whole directory tree, add a -p1 to your patch command:

patch -p1 <patch_file

Here is a concrete example of applying a patch to your Linux kernel source tree:

cd /usr/src
patch -p1 <patch.linux-2.4.19-my_version

Congratulations, you are finished.

References

For more information on diff and patch, see:

Patches