Last updated 12 March 2016.

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, including the codebase and database; and you have tested that backup First. While patching itself is relatively easy, it is important that you fully understand that a patch can possibly lead to the loss of data and/or site instabilities.

In fact, the ideal way to proceed is to download a backup of your production site, make a second copy of it on your computer, and test the second backup on your computer to make sure it works, and that you know how to do it. Then, patch that test site, test the patch results, and then upload the changes to your production site.

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 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:

You can use Git to apply a patch to a project's repository:

git apply -v path/file.patch

You can also use --index setting to track modified files:

git apply -v --index path/file.patch

If you are not using git, or if the repo isn't a local checkout of the project you wish to patch:

patch -p1 < 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. Patches created before git might need to be applied with: patch -p1 < path/file.patch

You can find more information 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.


allisterbeharry’s picture

Wikipedia has a comprehensive entry on patch including history and usage:
If you're wondering what the p parameter is for; from the man pages for patch at :

-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


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


without the leading slash, -p4 gives


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. contains a list of all parameters for patch.

ginc’s picture

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:

rpanna’s picture

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/'

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:


john.kenney’s picture

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:

EvanDonovan’s picture

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.

Nonprofit / EdTech Mentor

DropDeadFred’s picture

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...

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. ;)

Vale80’s picture

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!

hutch’s picture

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
Arwystli Networks

spiritcapsule’s picture

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?

pillarsdotnet’s picture

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

CVS-style patches:
Git-style patches:

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).
The filename of the patch you just downloaded or copied.

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

dshumaker’s picture

So features has this new patch I want to apply.

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 <

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!

pillarsdotnet’s picture

If you have ssh access, you can do this:

curl | ssh MYSERVER patch /var/www/MYSITE/sites/all/modules/contrib/features/features.module

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

ledom’s picture

What about patches with multiple files references?

Jenechka’s picture

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!


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


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

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

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/ 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>'

fadgadget’s picture

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.

varuntaliyan’s picture

Hi fadgadget,

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

Stagger Lee’s picture

I tried all of this GUI tools named here, but no success. Then i discovered free tool Aptana Studio.

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.

hermes_costell’s picture

  • Save the .patch file to your local computer (doesn't matter where)
  • Look in the .patch file and figure out where in the directory hierarchy it's supposed to be applied to
  • In your Projects pane right-click on that folder and select Tools -> Apply Diff Patch. Or if your project has a Git repo set up you can choose Git -> Patches -> Apply Diff Patch... pretty sure there's no difference.
  • If it tells you "The patch cannot be applied in the selected context." then you chose the wrong folder to apply it to. Typically if it's a patch for a module or theme then you're going to want to right-click the base folder of that module or theme.
  • You're done!
  • In NetBeans 8.2... but it should be the same in other versions
crobicha’s picture

Is it possible to apply a patch from inside of a module hook?

For example:

function module_enable() {
exec("git apply bug_fix.patch");

ressa’s picture

If I patch Drupal core and do an update, that patch is gone - is there a strategy for this situation?

Glossary in the Views module, which is now part of Drupal 8, was recently patched, but the patch didn't make it into Drupal 8.2...

Try Drupal 8 at
ressa’s picture

I found a possible solution in the article How To Manage Your Drupal Patches with 'Drush Patch File'.

Try Drupal 8 at
cwsecurity’s picture

Thanks for sharing the information. if anyone looking for free patch management software. check here to get patch management software.