[back to main page]
I send many patches to various projects. Many times I was asked "What should I do with this patch file you gave me?".
Here's my answer:
First of all, patches are just specially formatted text files.
Before applying a patch you should open it in any text editor,
so that you can see what the patch actually changes.
Note for Windows users: you should use an editor that honors
Unix line endings, which usually means that you can use
any editor besides that poor
You can read diffutils manual to get familiar with patch files format. In particular, see the description of the unified format, this is the most commonly used format.
For simple few-line patches it's indeed possible to just read them and "apply by hand", i.e. manually do all the changes that the patch describes.
For larger patches you will want to use a program that applies them to your source files. This will make applying the patch faster and less error-prone.
The traditional program to do this is the
patch command-line program.
Most people that ask questions about patches are Windows users,
and in this case you can get the
and other related programs, commonly known as
If you're under Unix (Linux, FreeBSD, etc.) then diffutils programs (most probably as implemented by GNU) are already installed and available on your $PATH.
The simple usage instructions: usually you should place the
in the top-level directory of your project, and then run from command-line
patch -p0 < file.patch. Sometimes it's
-p1 instead of
and sometimes no
-p* is needed. Don't be afraid to experiment:
the patch program will output some information how the process goes,
and it will stop if the file cannot be found (e.g. because you used wrong
option). Of course, having a backup of your files (or keeping them in
some version control system)
is always a good idea.
of GNU diffutils. In particular see
extensive diffutils manual how to use all the included programs,
you install diffutils you should also have this documentation available
locally somewhere (call
There are also programs that allow you to view and apply patches in an interactive manner, which is more comfortable than using command-line utilities:
C-h mwill give you help. Press
C-c C-ato apply hunk of the patch, this will show in another buffer the affected file.