Git fork
1[[generate_patch_text_with_p]]
2Generating patch text with -p
3-----------------------------
4
5Running
6linkgit:git-diff[1],
7linkgit:git-log[1],
8linkgit:git-show[1],
9linkgit:git-diff-index[1],
10linkgit:git-diff-tree[1], or
11linkgit:git-diff-files[1]
12with the `-p` option produces patch text.
13You 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
17What the `-p` option produces is slightly different from the traditional
18diff format:
19
201. It is preceded by a "git diff" header that looks like this:
21
22 diff --git a/file1 b/file2
23+
24The `a/` and `b/` filenames are the same unless rename/copy is
25involved. Especially, even for a creation or a deletion,
26`/dev/null` is _not_ used in place of the `a/` or `b/` filenames.
27+
28When a rename/copy is involved, `file1` and `file2` show the
29name of the source file of the rename/copy and the name of
30the file that the rename/copy produces, respectively.
31
322. It is followed by one or more extended header lines:
33+
34[synopsis]
35old mode <mode>
36new mode <mode>
37deleted file mode <mode>
38new file mode <mode>
39copy from <path>
40copy to <path>
41rename from <path>
42rename to <path>
43similarity index <number>
44dissimilarity index <number>
45index <hash>..<hash> <mode>
46+
47File modes _<mode>_ are printed as 6-digit octal numbers including the file type
48and file permission bits.
49+
50Path names in extended headers do not include the `a/` and `b/` prefixes.
51+
52The similarity index is the percentage of unchanged lines, and
53the dissimilarity index is the percentage of changed lines. It
54is a rounded down integer, followed by a percent sign. The
55similarity index value of 100% is thus reserved for two equal
56files, while 100% dissimilarity means that no line from the old
57file made it into the new one.
58+
59The index line includes the blob object names before and after the change.
60The _<mode>_ is included if the file mode does not change; otherwise,
61separate lines indicate the old and the new mode.
62
633. Pathnames with "unusual" characters are quoted as explained for
64 the configuration variable `core.quotePath` (see
65 linkgit:git-config[1]).
66
674. 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
795. 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
85Combined diff format
86--------------------
87
88Any diff-generating command can take the `-c` or `--cc` option to
89produce a 'combined diff' when showing a merge. This is the default
90format when showing merges with linkgit:git-diff[1] or
91linkgit:git-show[1]. Note also that you can give suitable
92`--diff-merges` option to any of these commands to force generation of
93diffs in a specific format.
94
95A "combined diff" format looks like this:
96
97------------
98diff --combined describe.c
99index 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
1271. 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+
132or like this (when the `--cc` option is used):
133
134 diff --cc file
135
1362. It is followed by one or more extended header lines
137 (this example shows a merge with two parents):
138+
139[synopsis]
140index <hash>,<hash>..<hash>
141mode <mode>,<mode>..<mode>
142new file mode <mode>
143deleted file mode <mode>,<mode>
144+
145The `mode <mode>,<mode>..<mode>` line appears only if at least one of
146the <mode> is different from the rest. Extended headers with
147information about detected content movement (renames and
148copying detection) are designed to work with the diff of two
149_<tree-ish>_ and are not used by combined diff format.
150
1513. It is followed by a two-line from-file/to-file header:
152
153 --- a/file
154 +++ b/file
155+
156Similar to the two-line header for the traditional 'unified' diff
157format, `/dev/null` is used to signal created or deleted
158files.
159+
160However, if the --combined-all-paths option is provided, instead of a
161two-line from-file/to-file, you get an N+1 line from-file/to-file header,
162where N is the number of parents in the merge commit:
163
164 --- a/file
165 --- a/file
166 --- a/file
167 +++ b/file
168+
169This extended format can be useful if rename or copy detection is
170active, to allow you to see the original name of the file in different
171parents.
172
1734. 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+
181There are (number of parents + 1) `@` characters in the chunk
182header for combined diff format.
183
184Unlike the traditional 'unified' diff format, which shows two
185files A and B with a single column that has `-` (minus --
186appears in A but removed in B), `+` (plus -- missing in A but
187added to B), or `" "` (space -- unchanged) prefix, this format
188compares two or more files file1, file2,... with one file X, and
189shows how X differs from each of fileN. One column for each of
190fileN is prepended to the output line to note how X's line is
191different from it.
192
193A `-` character in the column N means that the line appears in
194fileN but it does not appear in the result. A `+` character
195in the column N means that the line appears in the result,
196and fileN does not have that line (in other words, the line was
197added, from the point of view of that parent).
198
199In the above example output, the function signature was changed
200from both files (hence two `-` removals from both file1 and
201file2, plus `++` to mean one line that was added does not appear
202in either file1 or file2). Also, eight other lines are the same
203from file1 but do not appear in file2 (hence prefixed with `+`).
204
205When shown by `git diff-tree -c`, it compares the parents of a
206merge commit with the merge result (i.e. file1..fileN are the
207parents). When shown by `git diff-files -c`, it compares the
208two 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").