Documentation / diff-generate-patch.txton commit Merge branch 'nd/maint-relative' into maint (0bc6180)
   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 this:
  15
  16       diff --git a/file1 b/file2
  17+
  18The `a/` and `b/` filenames are the same unless rename/copy is
  19involved.  Especially, even for a creation or a deletion,
  20`/dev/null` is _not_ used in place of the `a/` or `b/` filenames.
  21+
  22When rename/copy is involved, `file1` and `file2` show the
  23name of the source file of the rename/copy and the name of
  24the file that rename/copy produces, respectively.
  25
  262.   It is followed by one or more extended header lines:
  27
  28       old mode <mode>
  29       new mode <mode>
  30       deleted file mode <mode>
  31       new file mode <mode>
  32       copy from <path>
  33       copy to <path>
  34       rename from <path>
  35       rename to <path>
  36       similarity index <number>
  37       dissimilarity index <number>
  38       index <hash>..<hash> <mode>
  39+
  40File modes are printed as 6-digit octal numbers including the file type
  41and file permission bits.
  42+
  43Path names in extended headers do not include the `a/` and `b/` prefixes.
  44+
  45The similarity index is the percentage of unchanged lines, and
  46the dissimilarity index is the percentage of changed lines.  It
  47is a rounded down integer, followed by a percent sign.  The
  48similarity index value of 100% is thus reserved for two equal
  49files, while 100% dissimilarity means that no line from the old
  50file made it into the new one.
  51+
  52The index line includes the SHA-1 checksum before and after the change.
  53The <mode> is included if the file mode does not change; otherwise,
  54separate lines indicate the old and the new mode.
  55
  563.  TAB, LF, double quote and backslash characters in pathnames
  57    are represented as `\t`, `\n`, `\"` and `\\`, respectively.
  58    If there is need for such substitution then the whole
  59    pathname is put in double quotes.
  60
  614.  All the `file1` files in the output refer to files before the
  62    commit, and all the `file2` files refer to files after the commit.
  63    It is incorrect to apply each change to each file sequentially.  For
  64    example, this patch will swap a and b:
  65
  66      diff --git a/a b/b
  67      rename from a
  68      rename to b
  69      diff --git a/b b/a
  70      rename from b
  71      rename to a
  72
  73
  74combined diff format
  75--------------------
  76
  77"git-diff-tree", "git-diff-files" and "git-diff" can take '-c' or
  78'--cc' option to produce 'combined diff'.  For showing a merge commit
  79with "git log -p", this is the default format; you can force showing
  80full diff with the '-m' option.
  81A 'combined diff' format looks like this:
  82
  83------------
  84diff --combined describe.c
  85index fabadb8,cc95eb0..4866510
  86--- a/describe.c
  87+++ b/describe.c
  88@@@ -98,20 -98,12 +98,20 @@@
  89        return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
  90  }
  91
  92- static void describe(char *arg)
  93 -static void describe(struct commit *cmit, int last_one)
  94++static void describe(char *arg, int last_one)
  95  {
  96 +      unsigned char sha1[20];
  97 +      struct commit *cmit;
  98        struct commit_list *list;
  99        static int initialized = 0;
 100        struct commit_name *n;
 101
 102 +      if (get_sha1(arg, sha1) < 0)
 103 +              usage(describe_usage);
 104 +      cmit = lookup_commit_reference(sha1);
 105 +      if (!cmit)
 106 +              usage(describe_usage);
 107 +
 108        if (!initialized) {
 109                initialized = 1;
 110                for_each_ref(get_name);
 111------------
 112
 1131.   It is preceded with a "git diff" header, that looks like
 114     this (when '-c' option is used):
 115
 116       diff --combined file
 117+
 118or like this (when '--cc' option is used):
 119
 120       diff --cc file
 121
 1222.   It is followed by one or more extended header lines
 123     (this example shows a merge with two parents):
 124
 125       index <hash>,<hash>..<hash>
 126       mode <mode>,<mode>..<mode>
 127       new file mode <mode>
 128       deleted file mode <mode>,<mode>
 129+
 130The `mode <mode>,<mode>..<mode>` line appears only if at least one of
 131the <mode> is different from the rest. Extended headers with
 132information about detected contents movement (renames and
 133copying detection) are designed to work with diff of two
 134<tree-ish> and are not used by combined diff format.
 135
 1363.   It is followed by two-line from-file/to-file header
 137
 138       --- a/file
 139       +++ b/file
 140+
 141Similar to two-line header for traditional 'unified' diff
 142format, `/dev/null` is used to signal created or deleted
 143files.
 144
 1454.   Chunk header format is modified to prevent people from
 146     accidentally feeding it to `patch -p1`. Combined diff format
 147     was created for review of merge commit changes, and was not
 148     meant for apply. The change is similar to the change in the
 149     extended 'index' header:
 150
 151       @@@ <from-file-range> <from-file-range> <to-file-range> @@@
 152+
 153There are (number of parents + 1) `@` characters in the chunk
 154header for combined diff format.
 155
 156Unlike the traditional 'unified' diff format, which shows two
 157files A and B with a single column that has `-` (minus --
 158appears in A but removed in B), `+` (plus -- missing in A but
 159added to B), or `" "` (space -- unchanged) prefix, this format
 160compares two or more files file1, file2,... with one file X, and
 161shows how X differs from each of fileN.  One column for each of
 162fileN is prepended to the output line to note how X's line is
 163different from it.
 164
 165A `-` character in the column N means that the line appears in
 166fileN but it does not appear in the result.  A `+` character
 167in the column N means that the line appears in the result,
 168and fileN does not have that line (in other words, the line was
 169added, from the point of view of that parent).
 170
 171In the above example output, the function signature was changed
 172from both files (hence two `-` removals from both file1 and
 173file2, plus `++` to mean one line that was added does not appear
 174in either file1 nor file2).  Also eight other lines are the same
 175from file1 but do not appear in file2 (hence prefixed with `{plus}`).
 176
 177When shown by `git diff-tree -c`, it compares the parents of a
 178merge commit with the merge result (i.e. file1..fileN are the
 179parents).  When shown by `git diff-files -c`, it compares the
 180two unresolved merge parents with the working tree file
 181(i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka
 182"their version").