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