blob: 231105cff48d9c109b2001623c67d51690c595ba [file] [log] [blame]
Junio C Hamano00b8b632007-12-07 09:50:491Generating patches with -p
2--------------------------
3
4When "git-diff-index", "git-diff-tree", or "git-diff-files" are run
Junio C Hamano92d80372016-07-13 22:00:055with a `-p` option, "git diff" without the `--raw` option, or
Junio C Hamano00b8b632007-12-07 09:50:496"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
Junio C Hamano042f2142016-06-27 18:05:059`GIT_EXTERNAL_DIFF` and the `GIT_DIFF_OPTS` environment variables.
Junio C Hamano00b8b632007-12-07 09:50:4910
11What the -p option produces is slightly different from the traditional
Junio C Hamano7f9fa862010-10-19 23:51:1412diff format:
Junio C Hamano00b8b632007-12-07 09:50:4913
Junio C Hamano7f9fa862010-10-19 23:51:14141. It is preceded with a "git diff" header that looks like this:
Junio C Hamano00b8b632007-12-07 09:50:4915
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,
Junio C Hamano7f9fa862010-10-19 23:51:1420`/dev/null` is _not_ used in place of the `a/` or `b/` filenames.
Junio C Hamano00b8b632007-12-07 09:50:4921+
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>
Junio C Hamano7f9fa862010-10-19 23:51:1439+
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+
Junio C Hamano00b8b632007-12-07 09:50:4945The 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.
Junio C Hamano7f9fa862010-10-19 23:51:1451+
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
Junio C Hamano0f6f3bf2017-03-13 07:01:53563. Pathnames with "unusual" characters are quoted as explained for
57 the configuration variable `core.quotePath` (see
58 linkgit:git-config[1]).
Junio C Hamano7f9fa862010-10-19 23:51:1459
604. All the `file1` files in the output refer to files before the
61 commit, and all the `file2` files refer to files after the commit.
62 It is incorrect to apply each change to each file sequentially. For
63 example, this patch will swap a and b:
64
65 diff --git a/a b/b
66 rename from a
67 rename to b
68 diff --git a/b b/a
69 rename from b
70 rename to a
Junio C Hamano00b8b632007-12-07 09:50:4971
72
73combined diff format
74--------------------
75
Junio C Hamanoc5be4cc2011-03-09 07:20:4376Any diff-generating command can take the `-c` or `--cc` option to
77produce a 'combined diff' when showing a merge. This is the default
78format when showing merges with linkgit:git-diff[1] or
Junio C Hamano1dbca522015-05-22 20:48:5579linkgit:git-show[1]. Note also that you can give the `-m` option to any
Junio C Hamanoc5be4cc2011-03-09 07:20:4380of these commands to force generation of diffs with individual parents
81of a merge.
82
Junio C Hamano00b8b632007-12-07 09:50:4983A 'combined diff' format looks like this:
84
85------------
86diff --combined describe.c
87index fabadb8,cc95eb0..4866510
88--- a/describe.c
89+++ b/describe.c
90@@@ -98,20 -98,12 +98,20 @@@
91return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
92 }
93
94- static void describe(char *arg)
95 -static void describe(struct commit *cmit, int last_one)
96++static void describe(char *arg, int last_one)
97 {
98 + unsigned char sha1[20];
99 + struct commit *cmit;
100struct commit_list *list;
101static int initialized = 0;
102struct commit_name *n;
103
104 + if (get_sha1(arg, sha1) < 0)
105 + usage(describe_usage);
106 + cmit = lookup_commit_reference(sha1);
107 + if (!cmit)
108 + usage(describe_usage);
109 +
110if (!initialized) {
111initialized = 1;
112for_each_ref(get_name);
113------------
114
1151. It is preceded with a "git diff" header, that looks like
Junio C Hamano92d80372016-07-13 22:00:05116 this (when `-c` option is used):
Junio C Hamano00b8b632007-12-07 09:50:49117
118 diff --combined file
119+
Junio C Hamano92d80372016-07-13 22:00:05120or like this (when `--cc` option is used):
Junio C Hamano00b8b632007-12-07 09:50:49121
Junio C Hamano0868a302008-07-22 09:20:44122 diff --cc file
Junio C Hamano00b8b632007-12-07 09:50:49123
1242. It is followed by one or more extended header lines
125 (this example shows a merge with two parents):
126
127 index <hash>,<hash>..<hash>
128 mode <mode>,<mode>..<mode>
129 new file mode <mode>
130 deleted file mode <mode>,<mode>
131+
132The `mode <mode>,<mode>..<mode>` line appears only if at least one of
133the <mode> is different from the rest. Extended headers with
134information about detected contents movement (renames and
135copying detection) are designed to work with diff of two
136<tree-ish> and are not used by combined diff format.
137
1383. It is followed by two-line from-file/to-file header
139
140 --- a/file
141 +++ b/file
142+
143Similar to two-line header for traditional 'unified' diff
144format, `/dev/null` is used to signal created or deleted
145files.
146
1474. Chunk header format is modified to prevent people from
148 accidentally feeding it to `patch -p1`. Combined diff format
149 was created for review of merge commit changes, and was not
150 meant for apply. The change is similar to the change in the
151 extended 'index' header:
152
153 @@@ <from-file-range> <from-file-range> <to-file-range> @@@
154+
155There are (number of parents + 1) `@` characters in the chunk
156header for combined diff format.
157
158Unlike the traditional 'unified' diff format, which shows two
159files A and B with a single column that has `-` (minus --
160appears in A but removed in B), `+` (plus -- missing in A but
161added to B), or `" "` (space -- unchanged) prefix, this format
162compares two or more files file1, file2,... with one file X, and
163shows how X differs from each of fileN. One column for each of
164fileN is prepended to the output line to note how X's line is
165different from it.
166
167A `-` character in the column N means that the line appears in
168fileN but it does not appear in the result. A `+` character
Junio C Hamano54bf1e22008-12-20 06:30:11169in the column N means that the line appears in the result,
Junio C Hamano00b8b632007-12-07 09:50:49170and fileN does not have that line (in other words, the line was
171added, from the point of view of that parent).
172
173In the above example output, the function signature was changed
174from both files (hence two `-` removals from both file1 and
175file2, plus `++` to mean one line that was added does not appear
Junio C Hamanod75148a2014-04-08 19:48:38176in either file1 or file2). Also eight other lines are the same
Junio C Hamanob76a6862012-05-02 22:02:46177from file1 but do not appear in file2 (hence prefixed with `+`).
Junio C Hamano00b8b632007-12-07 09:50:49178
179When shown by `git diff-tree -c`, it compares the parents of a
180merge commit with the merge result (i.e. file1..fileN are the
181parents). When shown by `git diff-files -c`, it compares the
182two unresolved merge parents with the working tree file
183(i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka
184"their version").