How to make a patch file

February 18th, 2004

This post is in the “things which should have been easy for someone to find with Google but aren’t” department, because sometimes you’d like to be able to tell someone DTFG, but you can’t. (previous examples included how to do conditional GETs in Perl). All material is lifted from excellent patch man pages, see “Notes for Patch Senders”.

Whats a patch?

A patch is the best and easiest way to submit changes back to an open source project. It’s a summary of changes you made to file or files formatted in a way that can easily be used by the excellent program, named, unsurprisingly, ‘patch’. Now because patch was written by the inestimable Larry Wall, patches can come in a wide range of shapes, sizes, and formats, and they will apply with a high degree of “do what I mean”-ness. However, there are some tips to produce high quality patches.

How to: the Short Answer

diff -Naur old new > patch_file

e.g.

diff -Naur rss_parse.inc rss_fetch_gopher.inc > add_gopher_support.patch

Slightly Longer Answer

The diff command’s headers should have dates and times in Universal Time using traditional Unix format, so that patch recipients can use the -Z or –set-utc option. Here is an example command, using Bourne shell syntax:

TZ=UTC0 diff -Naur gcc-2.7 gcc-2.8

Tips for Generating Usable Patch Files

  1. Tell your recipients how to apply the patch by telling them which directory to cd to, and which patch options to use.
  2. State clearly what the patch is suppose to accomplish, and which version it is meant to be applied to.
  3. Your patch will be easier to apply if you place the original and patched sources next to each other in the file hierarchy so that the path depths to the files will be the same.
  4. You can produce patches for entire directories of files, not just a single file, however try to keep your patches relatively straightforward, and thematically linked. This makes it easier to understand when something goes wrong.
  5. I’m sure there are others. Did I miss your favorite?

Code improvements, and bug fixes are great, are wonderful, but patches make it much much easier to use them.

10 Responses to “How to make a patch file”

  1. skzaed says:

    i want patch file file for file anti copy software

  2. sourish roy says:

    i want to make patch file to crack any type of software.plz tell me the process.

  3. Davis says:

    Presuming the sources are in CVS (or Subversion) can you show how one could create a patch to repeat the local edits?

    “cvs diff -U5″ seems to be most of what I need, but not quite.

    Thanks.

  4. ross says:

    Thanks very much for this article. A google search for, “how to make a .patch” (without quotes) had this in the #1 position. Saved me the effort of reading a man page.

  5. Ismael says:

    Thank you very much, very useful

  6. Michael says:

    Simply a great hint.

  7. Tim says:

    Cheers – Helped me with making a patch for some Drupal code.

  8. DeRoyd says:

    I need to patch my game server version to 4.17 , the game is Freelancer can you help me ? pls mail me

  9. taskee says:

    I would like to make beyluxe messenger multi with patch how can do it plz help me

  10. Haha says:

    These replies are totally cracking me up.

    You post a way of creating a text file, with the difference between two files… And people want you to help crack software or patch game servers.

    I found a way to remove the cap from my pen. Should I help you write up your Ph.D. thesis?