Help with Patch Files

Example Patch File

Here is an example patch file (aka "diff" or "patch"):

Index: tmp/hello.c
===================================================================
--- tmp.orig/hello.c        2009-04-22 09:59:28.707583382 -0600
+++ tmp/hello.c     2009-04-22 10:00:06.432872620 -0600
@@ -3,6 +3,6 @@
 int
 main ()
 {
-    printf ("Hello world\n");
+    printf ("Hello Doctor Who\n");
     return 0;
 }

The patch file indicates to perform the instructions shown in the following figure:

im-edited/diff-example.png

Syntax of a Patch File

Patch files are divided in two parts: (1) headers, and (2) hunks. The header forms the first few lines, and the hunk starts with an "@@" symbol. Here is how to interpret a diff file:

  1. In the header, if the line starts with:

    • "---"

      This specifies the original file name (prior to being patched). Usually, this filename is ignored. The space between the filename and optional timestamp is supposed to be a tab character ("t").

    • "+++"

      This is the new file name that is going to be patched. The space between the filename and optional timestamp is supposed to be a tab character ("\t").

  2. In the hunk (delimited by "@@"), if the line starts with:

    • " " (just a single space character)

      This is called a "context line". It doesn't result in any operation happening. But it does help you figure out how to locate other lines where operations do happen.

    • "-"

      This is a delete operation. The line indiciated by the "-" is to be removed.

    • "+"

      This is an insert operation. The line indiciated by the "+" is to be inserted.