Documentation / diff-generate-patch.txton commit Add 'g' command to go to a hunk (070434d)
   1Generating patches with -p
   2--------------------------
   3
   4When "git-diff-index", "git-diff-tree", or "git-diff-files" are run
   5with a '-p' option, "git diff" without the '--raw' option, or
   6"git log" with the "-p" option, they
   7do not produce the output described above; instead they produce a
   8patch file.  You can customize the creation of such patches via the
   9GIT_EXTERNAL_DIFF and the GIT_DIFF_OPTS environment variables.
  10
  11What the -p option produces is slightly different from the traditional
  12diff format.
  13
  141.   It is preceded with a "git diff" header, that looks like
  15     this:
  16
  17       diff --git a/file1 b/file2
  18+
  19The `a/` and `b/` filenames are the same unless rename/copy is
  20involved.  Especially, even for a creation or a deletion,
  21`/dev/null` is _not_ used in place of `a/` or `b/` filenames.
  22+
  23When rename/copy is involved, `file1` and `file2` show the
  24name of the source file of the rename/copy and the name of
  25the file that rename/copy produces, respectively.
  26
  272.   It is followed by one or more extended header lines:
  28
  29       old mode <mode>
  30       new mode <mode>
  31       deleted file mode <mode>
  32       new file mode <mode>
  33       copy from <path>
  34       copy to <path>
  35       rename from <path>
  36       rename to <path>
  37       similarity index <number>
  38       dissimilarity index <number>
  39       index <hash>..<hash> <mode>
  40
  413.  TAB, LF, double quote and backslash characters in pathnames
  42    are represented as `\t`, `\n`, `\"` and `\\`, respectively.
  43    If there is need for such substitution then the whole
  44    pathname is put in double quotes.
  45
  46The similarity index is the percentage of unchanged lines, and
  47the dissimilarity index is the percentage of changed lines.  It
  48is a rounded down integer, followed by a percent sign.  The
  49similarity index value of 100% is thus reserved for two equal
  50files, while 100% dissimilarity means that no line from the old
  51file made it into the new one.
  52
  53
  54combined diff format
  55--------------------
  56
  57"git-diff-tree", "git-diff-files" and "git-diff" can take '-c' or
  58'--cc' option to produce 'combined diff'.  For showing a merge commit
  59with "git log -p", this is the default format.
  60A 'combined diff' format looks like this:
  61
  62------------
  63diff --combined describe.c
  64index fabadb8,cc95eb0..4866510
  65--- a/describe.c
  66+++ b/describe.c
  67@@@ -98,20 -98,12 +98,20 @@@
  68        return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
  69  }
  70
  71- static void describe(char *arg)
  72 -static void describe(struct commit *cmit, int last_one)
  73++static void describe(char *arg, int last_one)
  74  {
  75 +      unsigned char sha1[20];
  76 +      struct commit *cmit;
  77        struct commit_list *list;
  78        static int initialized = 0;
  79        struct commit_name *n;
  80
  81 +      if (get_sha1(arg, sha1) < 0)
  82 +              usage(describe_usage);
  83 +      cmit = lookup_commit_reference(sha1);
  84 +      if (!cmit)
  85 +              usage(describe_usage);
  86 +
  87        if (!initialized) {
  88                initialized = 1;
  89                for_each_ref(get_name);
  90------------
  91
  921.   It is preceded with a "git diff" header, that looks like
  93     this (when '-c' option is used):
  94
  95       diff --combined file
  96+
  97or like this (when '--cc' option is used):
  98
  99       diff --cc file
 100
 1012.   It is followed by one or more extended header lines
 102     (this example shows a merge with two parents):
 103
 104       index <hash>,<hash>..<hash>
 105       mode <mode>,<mode>..<mode>
 106       new file mode <mode>
 107       deleted file mode <mode>,<mode>
 108+
 109The `mode <mode>,<mode>..<mode>` line appears only if at least one of
 110the <mode> is different from the rest. Extended headers with
 111information about detected contents movement (renames and
 112copying detection) are designed to work with diff of two
 113<tree-ish> and are not used by combined diff format.
 114
 1153.   It is followed by two-line from-file/to-file header
 116
 117       --- a/file
 118       +++ b/file
 119+
 120Similar to two-line header for traditional 'unified' diff
 121format, `/dev/null` is used to signal created or deleted
 122files.
 123
 1244.   Chunk header format is modified to prevent people from
 125     accidentally feeding it to `patch -p1`. Combined diff format
 126     was created for review of merge commit changes, and was not
 127     meant for apply. The change is similar to the change in the
 128     extended 'index' header:
 129
 130       @@@ <from-file-range> <from-file-range> <to-file-range> @@@
 131+
 132There are (number of parents + 1) `@` characters in the chunk
 133header for combined diff format.
 134
 135Unlike the traditional 'unified' diff format, which shows two
 136files A and B with a single column that has `-` (minus --
 137appears in A but removed in B), `+` (plus -- missing in A but
 138added to B), or `" "` (space -- unchanged) prefix, this format
 139compares two or more files file1, file2,... with one file X, and
 140shows how X differs from each of fileN.  One column for each of
 141fileN is prepended to the output line to note how X's line is
 142different from it.
 143
 144A `-` character in the column N means that the line appears in
 145fileN but it does not appear in the result.  A `+` character
 146in the column N means that the line appears in the last file,
 147and fileN does not have that line (in other words, the line was
 148added, from the point of view of that parent).
 149
 150In the above example output, the function signature was changed
 151from both files (hence two `-` removals from both file1 and
 152file2, plus `++` to mean one line that was added does not appear
 153in either file1 nor file2).  Also two other lines are the same
 154from file1 but do not appear in file2 (hence prefixed with ` +`).
 155
 156When shown by `git diff-tree -c`, it compares the parents of a
 157merge commit with the merge result (i.e. file1..fileN are the
 158parents).  When shown by `git diff-files -c`, it compares the
 159two unresolved merge parents with the working tree file
 160(i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka
 161"their version").