Gentoo Wiki


Generating patches, files containing the difference between files, is the domain of diff programs. There are many different programs with this functionality; some stand-alone (diff), some integrated in IDE's (Eclipse, Xcode) or version control systems (CVS).

This page only deals with some basic principles using the command line utility diff. Guidelines for patch submission to the issue tracker can be found in HOWTO: Submit patches for Drupal.

Diff can be found on most UNIX systems and is included in the packages UnxUtils and Cygwin for use on Windows.

Standard diff

Although diff can output in diverse formats, the easiest to read is the unified format; simply use the -u switch to generate unified output.

diff -u old.php new.php > file.patch

Note: the symbol > will redirect the output to the file file.patch

Because patch readability is important for the review process it's best to add another switch: -p. This switch shows the function closest to the difference, making it easy to see what function changed. (alternately you can use the flag -F^function or in abbreviated form -F^f)

diff -up old.php new.php > file.patch

When you've modified multiple files in the source tree, use diffs ability to compare directories. Add the -r switch to instruct diff to recurse (sub)directories and add the -N switch to account for new or deleted files:

diff -urNp old_directory new_directory > file.patch

Applying the patch

You can apply the patch to a single file with the command:

patch -p0 old.php < file.patch

Or to the whole subdirectory without defining the file:

patch -p0 < file.patch

The headers in the diff must match the files you are patching.

Creating a patch to include newly added files against cvs

When executing the command:

cvs -z3 diff -NuF^f > patchfile.patch

in your cvsroot with files that you have newly added your .patch file will have entries like this:

? modules/user/user.meta
? modules/watchdog/watchdog.meta

This is because these files cannot be compared with files in the repository because CVS doesn't know they exist. For this reason you will have to tell CVS that these files are added by editing the ./CVS/Entries file in the directory where the new files are.

/menu.css/1.3/Wed Aug 16 07:32:16 2006//
/menu.module/1.81/Sun Aug 20 08:03:58 2006//
/menu.meta/0/Initial menu.meta//
/menu.install/0/Initial menu.install//

In this example we added the two new files menu.meta and menu.install giving them a version 0 and a comment that they are new. If you run cvs diff again you will see the contents of the new files in your patch file which can be patched by others who want to review your contribution.

Retrieved from ""

Last modified: Fri, 05 Sep 2008 06:39:00 +0000 Hits: 732