Last updated September 6, 2013.

This information deals with applying patches without git. For information on using git to apply patches, please see the git patch contributor guide. For more generic information about patches, please see the Patch section of the Getting Involved Guide.

Applying patches, modifying files according to instructions in the patch file, is the domain of patch programs. There are many different programs with this functionality, some stand-alone (patch), some integrated in IDEs (Eclipse, XCode).

Warning: Patching is something that should never be done on your production site unless you first have a complete backup of your site, and you have tested that backup: First. While patching itself is relatively easy, understanding the implications of a patch is not. Patching your system can lead to loss of data and/or site instabilities.

This page only deals with some basic principles using the command line utility patch. Patch can be found on most UNIX systems and is included in the packages UnxUtils and Cygwin for use on Windows. There is also a video on Applying and Creating patches with Git.

Provided that the patch was made relative to the root directory of the concerned project, navigate to the that directory (using cd). For a patch on Drupal, that will be the Drupal directory; for a contrib module or theme, that is the root directory of the project. Once there, issue the command:

patch -p1 < path/file.patch

or if you are using Git:
git apply --index path/file.patch

Note: git apply will fail to do anything when used within a local checkout of a git repository (other than the one for the project the patch is made for), such as if you are patching a module that is within a site that is in Git version control. Use patch -p1 < path/file.patch instead. Older patches from before git might need to be applied with: patch -p1 < path/file.patch

You can find more info about applying patches with Git in the Git handbook

The -p option tells patch how many leading prefixes to strip. For patches created using git, -p1 is normally the right option, and is the default for git apply. If that doesn't work, try either of the above commands with -p0 instead.

If the patch was not made relative to the project's root directory, you can place the patch in the same directory as the file being patched and run the patch command without the -p option. To do so, cd to the directory and type:

patch < file.patch

Patch will usually auto-detect the format. You can also use the -u command line option to indicate a unified patch, and the -b option creates a backup copy of the file before modifying it. In case of problems, you can then easily restore the backup file.

You can also reverse the patch if you want to.

Comments

Wikipedia has a comprehensive entry on patch including history and usage: http://en.wikipedia.org/wiki/Patch_%28Unix%29
If you're wondering what the p parameter is for; from the man pages for patch at http://www.die.net/doc/linux/man/man1/patch.1.html :

-pnum or --strip=num
Strip the smallest prefix containing num leading slashes from each file name found in the patch file. A sequence of one or more adjacent slashes is counted as a single slash. This controls how file names found in the patch file are treated, in case you keep your files in a different directory than the person who sent out the patch. For example, supposing the file name in the patch file was

/u/howard/src/blurfl/blurfl.c

setting -p0 gives the entire file name unmodified, -p1 gives

u/howard/src/blurfl/blurfl.c

without the leading slash, -p4 gives

blurfl/blurfl.c

and not specifying -p at all just gives you blurfl.c. Whatever you end up with is looked for either in the current directory, or the directory specified by the -d option.

http://www.die.net/doc/linux/man/man1/patch.1.html contains a list of all parameters for patch.

you can easily apply patches to your code using eclipse, here are the steps:

1) menu>windows>open perspective>others>team synchronizing
2) open the patch file, select all text and copy it to clipboard (Ctrl+ C)
3) menu>project>apply patch...
4) select clipboard, click next, select the file you want to patch, click finish or next to setup patching options.

to learn how to install and setup Eclipse for Drupal visit:
http://drupal.org/node/157609

When I run patches I usually run into a problem where the patch seems to mess up my site. However the problem comes from the fact that I have SELINUX enabled. When I run the patch it changes the security context for the patched file and it need to be reset.

I usually get an error message something like this:

PHP Fatal error:  require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'modules/node/node.pages.inc'

To change the context of the patch file go to that folder and run "ls -Z" to view the context of the files in the folder. Next copy the context from a file that was not patched to the patched files like this:

chcon --reference=filewithpropercontext.inc newlypatchedfile.inc

took me ages to understand the process described in these pages. many important details are not well described.

i wrote a baby steps version here: http://drupal.org/node/620014

pwolanin in #drupal IRC said that on Mac OS X, you can apply a patch using tab2space -unix -t2 foo.patch | patch -p0, and the code style of the patch will be fixed to use 2 spaces (as per the Drupal coding standards) rather than tabs. It will also have Unix-style line endings.

I didn't have shell access and wanted to run a patch directly on a new install on my production site. (SimpleScripts install, easy to redo, at BH)

I used the run.php from this tutorial and it worked like a charm...

http://php-html.net/tutorials/how-to-write-a-php-script-to-run-shell-com...

I was even able to apply a diff to the patch first using patch because the patch was wrong.

(How much patch could a diff patch patch if a diff patch could patch patch?)

Hint: Make sure you delete the run.php when you are done. ;)

Hello Fred,
I'm new in Drupal and I'm really getting lost. :)
I need to apply this patch Profile2 to a production site.
I have read the article-tutorial in your comment but I still don't get how to proceed.
So far is clear that I have to create a run.php file (and delete it afterwords).
Here are my doubts:
- Where should be placed the run.php file? In the main directory? Also if my Drupal is located in domain-name/drupal ?
- How do I apply this patch Profile2 with the tutorial you mentioned?
I would be very thankful if you can just point me out the right way :)
Thanks a lot!

Cheers,
VP

Now that Drupal is using git, there are some different methods to apply patches:

If the patch was made using 'git diff' and the patch is to be made on a git clone of the module concerned, then the patch should be applied with the command
'git apply [patchname]'

If the patch was made using 'git diff' and the patch is to be made on an unpacked tarball, then the patch should be applied with the command
'patch -p1 < [patchname]'

In both cases the patch should be placed in the module directory.

If you are not sure how a patch was made, open it and if it has paths beginning with a/ and b/ then it's a git style patch.

Old CVS style patches (and patches made with diff) would be applied with 'patch -p0 < [patchname]' but they should no longer appear.

Hope this helps

-----------------
Bob Hutchinson
Midwales dot com
-----------------

We're using git submodules to include drupal modules in our sites and we maintain the repos for our sites on github. What's the best practice for including a patch on one of the submodules?

I was thinking about forking the module repository and then using git submodule to reference my fork rather than the original repo.

Is there a better way to do it?

Chris Patterson
UCLA

People who get confused about the "<" character and don't mind a little more typing may prefer this form:

CVS-style patches:
    patch -dPROJECT_DIR -p0 -iPATCHFILE
Git-style patches:
    patch -dPROJECT_DIR -p1 -iPATCHFILE

PROJECT_DIR
    The directory for the project you are patching. For example, /var/www (for Drupal core) or /var/www/sites/all/modules/mymodule (for the mymodule module).
PATCHFILE
    The filename of the patch you just downloaded or copied.

Good. — Fast. — Cheap.
(Pick any two.)

So features has this new patch I want to apply.

http://drupal.org/files/features-1635662-1.patch

Can you run patch on a remote file? I know that copying the above patch into a file, transferring that file to my server, then running the patch command is easy, but I'd really like to just be able to run:

patch /var/www/mysite/sites/all/modules/contrib/features/features.module < http://drupal.org/files/features-1635662-1.patch

and then be done with it.

Is anything like this possible? I don't see any command line options for patch that seem to handle such a thing. Perhaps drush could rescue us here?

thanks for any help!
-Dan

If you have ssh access, you can do this:

curl http://drupal.org/files/features-1635662-1.patch | ssh MYSERVER patch /var/www/MYSITE/sites/all/modules/contrib/features/features.module

Good. — Fast. — Cheap.
(Pick any two.)

What about patches with multiple files references?

Note: git apply will fail to do anything when used within a local checkout of a git repository...

For those who has no patch command and git apply does nothing. The solution is:
Let's modify the patch file!

From

diff --git a/uc_attribute/uc_attribute.admin.inc b/uc_attribute/uc_attribute.admin.inc
index b9a978a..ef33ca3 100644
--- a/uc_attribute/uc_attribute.admin.inc
+++ b/uc_attribute/uc_attribute.admin.inc

To

diff --git ubercart/uc_attribute/uc_attribute.admin.inc ubercart/uc_attribute/uc_attribute.admin.inc
index 1c35bf8..587fa67 100755
--- ubercart/uc_attribute/uc_attribute.admin.inc
+++ ubercart/uc_attribute/uc_attribute.admin.inc

Apply patch from project root using
git apply -p0 PATCHFILE.patch

Verbose:
You should replace 'a/' and 'b/' from patchfile with '<projectname>/' (In my example it is 'ubercart')

After applying patch you may see warning like
warning: ubercart/uc_attribute/uc_attribute.admin.inc has type 100755, expected 100644
It's OK, don't worry.

NOTE! If patch contains diffs for a several files you should replace ALL occurrences of 'a/<anypath>' and 'b/<anypath>'

For years i struggled with applying patches. Then i discovered Netbeans and it made it a breeze. Here's a quick screencast on how to do it and banish your patching woes.
http://www.youtube.com/watch?v=rQm6xI3VUWE

Hi fadgadget,

Thanks a lot for sharing the info ... u saved my day .. :)

I tried all of this GUI tools named here, but no success. Then i discovered free tool Aptana Studio.
http://www.aptana.com/

In "Project workspace" click right at patch, and Team--> Apply patch. No errors, no setup of stupid repository folder, and reverse option is there, in case you need it.

Some people use Aptana for everything for web, i use Adobe DW.

Aptana has one simple option, that made all other tools not work with some patches, "Ignore leading path name segments". I dont know why, one path had a/, and b/ in the beginning of paths, and that made other tools spit errors, and not proceed.

Maybe it was just my lack of experience with patches. Anyway Aptana was most easy for me, and it is full blooded code editor too. Completely free, and regularly updated.