diff-generate-patch.txt 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. Generating patch text with -p
  2. -----------------------------
  3. Running
  4. linkgit:git-diff[1],
  5. linkgit:git-log[1],
  6. linkgit:git-show[1],
  7. linkgit:git-diff-index[1],
  8. linkgit:git-diff-tree[1], or
  9. linkgit:git-diff-files[1]
  10. with the `-p` option produces patch text.
  11. You can customize the creation of patch text via the
  12. `GIT_EXTERNAL_DIFF` and the `GIT_DIFF_OPTS` environment variables
  13. (see linkgit:git[1]).
  14. What the -p option produces is slightly different from the traditional
  15. diff format:
  16. 1. It is preceded with a "git diff" header that looks like this:
  17. diff --git a/file1 b/file2
  18. +
  19. The `a/` and `b/` filenames are the same unless rename/copy is
  20. involved. Especially, even for a creation or a deletion,
  21. `/dev/null` is _not_ used in place of the `a/` or `b/` filenames.
  22. +
  23. When rename/copy is involved, `file1` and `file2` show the
  24. name of the source file of the rename/copy and the name of
  25. the file that rename/copy produces, respectively.
  26. 2. It is followed by one or more extended header lines:
  27. old mode <mode>
  28. new mode <mode>
  29. deleted file mode <mode>
  30. new file mode <mode>
  31. copy from <path>
  32. copy to <path>
  33. rename from <path>
  34. rename to <path>
  35. similarity index <number>
  36. dissimilarity index <number>
  37. index <hash>..<hash> <mode>
  38. +
  39. File modes are printed as 6-digit octal numbers including the file type
  40. and file permission bits.
  41. +
  42. Path names in extended headers do not include the `a/` and `b/` prefixes.
  43. +
  44. The similarity index is the percentage of unchanged lines, and
  45. the dissimilarity index is the percentage of changed lines. It
  46. is a rounded down integer, followed by a percent sign. The
  47. similarity index value of 100% is thus reserved for two equal
  48. files, while 100% dissimilarity means that no line from the old
  49. file made it into the new one.
  50. +
  51. The index line includes the blob object names before and after the change.
  52. The <mode> is included if the file mode does not change; otherwise,
  53. separate lines indicate the old and the new mode.
  54. 3. Pathnames with "unusual" characters are quoted as explained for
  55. the configuration variable `core.quotePath` (see
  56. linkgit:git-config[1]).
  57. 4. All the `file1` files in the output refer to files before the
  58. commit, and all the `file2` files refer to files after the commit.
  59. It is incorrect to apply each change to each file sequentially. For
  60. example, this patch will swap a and b:
  61. diff --git a/a b/b
  62. rename from a
  63. rename to b
  64. diff --git a/b b/a
  65. rename from b
  66. rename to a
  67. Combined diff format
  68. --------------------
  69. Any diff-generating command can take the `-c` or `--cc` option to
  70. produce a 'combined diff' when showing a merge. This is the default
  71. format when showing merges with linkgit:git-diff[1] or
  72. linkgit:git-show[1]. Note also that you can give the `-m` option to any
  73. of these commands to force generation of diffs with individual parents
  74. of a merge.
  75. A "combined diff" format looks like this:
  76. ------------
  77. diff --combined describe.c
  78. index fabadb8,cc95eb0..4866510
  79. --- a/describe.c
  80. +++ b/describe.c
  81. @@@ -98,20 -98,12 +98,20 @@@
  82. return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
  83. }
  84. - static void describe(char *arg)
  85. -static void describe(struct commit *cmit, int last_one)
  86. ++static void describe(char *arg, int last_one)
  87. {
  88. + unsigned char sha1[20];
  89. + struct commit *cmit;
  90. struct commit_list *list;
  91. static int initialized = 0;
  92. struct commit_name *n;
  93. + if (get_sha1(arg, sha1) < 0)
  94. + usage(describe_usage);
  95. + cmit = lookup_commit_reference(sha1);
  96. + if (!cmit)
  97. + usage(describe_usage);
  98. +
  99. if (!initialized) {
  100. initialized = 1;
  101. for_each_ref(get_name);
  102. ------------
  103. 1. It is preceded with a "git diff" header, that looks like
  104. this (when the `-c` option is used):
  105. diff --combined file
  106. +
  107. or like this (when the `--cc` option is used):
  108. diff --cc file
  109. 2. It is followed by one or more extended header lines
  110. (this example shows a merge with two parents):
  111. index <hash>,<hash>..<hash>
  112. mode <mode>,<mode>..<mode>
  113. new file mode <mode>
  114. deleted file mode <mode>,<mode>
  115. +
  116. The `mode <mode>,<mode>..<mode>` line appears only if at least one of
  117. the <mode> is different from the rest. Extended headers with
  118. information about detected contents movement (renames and
  119. copying detection) are designed to work with diff of two
  120. <tree-ish> and are not used by combined diff format.
  121. 3. It is followed by two-line from-file/to-file header
  122. --- a/file
  123. +++ b/file
  124. +
  125. Similar to two-line header for traditional 'unified' diff
  126. format, `/dev/null` is used to signal created or deleted
  127. files.
  128. +
  129. However, if the --combined-all-paths option is provided, instead of a
  130. two-line from-file/to-file you get a N+1 line from-file/to-file header,
  131. where N is the number of parents in the merge commit
  132. --- a/file
  133. --- a/file
  134. --- a/file
  135. +++ b/file
  136. +
  137. This extended format can be useful if rename or copy detection is
  138. active, to allow you to see the original name of the file in different
  139. parents.
  140. 4. Chunk header format is modified to prevent people from
  141. accidentally feeding it to `patch -p1`. Combined diff format
  142. was created for review of merge commit changes, and was not
  143. meant to be applied. The change is similar to the change in the
  144. extended 'index' header:
  145. @@@ <from-file-range> <from-file-range> <to-file-range> @@@
  146. +
  147. There are (number of parents + 1) `@` characters in the chunk
  148. header for combined diff format.
  149. Unlike the traditional 'unified' diff format, which shows two
  150. files A and B with a single column that has `-` (minus --
  151. appears in A but removed in B), `+` (plus -- missing in A but
  152. added to B), or `" "` (space -- unchanged) prefix, this format
  153. compares two or more files file1, file2,... with one file X, and
  154. shows how X differs from each of fileN. One column for each of
  155. fileN is prepended to the output line to note how X's line is
  156. different from it.
  157. A `-` character in the column N means that the line appears in
  158. fileN but it does not appear in the result. A `+` character
  159. in the column N means that the line appears in the result,
  160. and fileN does not have that line (in other words, the line was
  161. added, from the point of view of that parent).
  162. In the above example output, the function signature was changed
  163. from both files (hence two `-` removals from both file1 and
  164. file2, plus `++` to mean one line that was added does not appear
  165. in either file1 or file2). Also eight other lines are the same
  166. from file1 but do not appear in file2 (hence prefixed with `+`).
  167. When shown by `git diff-tree -c`, it compares the parents of a
  168. merge commit with the merge result (i.e. file1..fileN are the
  169. parents). When shown by `git diff-files -c`, it compares the
  170. two unresolved merge parents with the working tree file
  171. (i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka
  172. "their version").