Home Explore Blog CI



git

1st chunk of `Documentation/diff-generate-patch.adoc`
06b0ca02b552c28b7c76d7ae2bebdf175d98b8cc431c7df00000000100000e0f
[[generate_patch_text_with_p]]
Generating patch text with -p
-----------------------------

Running
linkgit:git-diff[1],
linkgit:git-log[1],
linkgit:git-show[1],
linkgit:git-diff-index[1],
linkgit:git-diff-tree[1], or
linkgit:git-diff-files[1]
with the `-p` option produces patch text.
You can customize the creation of patch text via the
`GIT_EXTERNAL_DIFF` and the `GIT_DIFF_OPTS` environment variables
(see linkgit:git[1]), and the `diff` attribute (see linkgit:gitattributes[5]).

What the `-p` option produces is slightly different from the traditional
diff format:

1.   It is preceded by a "git diff" header that looks like this:

       diff --git a/file1 b/file2
+
The `a/` and `b/` filenames are the same unless rename/copy is
involved.  Especially, even for a creation or a deletion,
`/dev/null` is _not_ used in place of the `a/` or `b/` filenames.
+
When a rename/copy is involved, `file1` and `file2` show the
name of the source file of the rename/copy and the name of
the file that the rename/copy produces, respectively.

2.   It is followed by one or more extended header lines:
+
[synopsis]
old mode <mode>
new mode <mode>
deleted file mode <mode>
new file mode <mode>
copy from <path>
copy to <path>
rename from <path>
rename to <path>
similarity index <number>
dissimilarity index <number>
index <hash>..<hash> <mode>
+
File modes _<mode>_ are printed as 6-digit octal numbers including the file type
and file permission bits.
+
Path names in extended headers do not include the `a/` and `b/` prefixes.
+
The similarity index is the percentage of unchanged lines, and
the dissimilarity index is the percentage of changed lines.  It
is a rounded down integer, followed by a percent sign.  The
similarity index value of 100% is thus reserved for two equal
files, while 100% dissimilarity means that no line from the old
file made it into the new one.
+
The index line includes the blob object names before and after the change.
The _<mode>_ is included if the file mode does not change; otherwise,
separate lines indicate the old and the new mode.

3.  Pathnames with "unusual" characters are quoted as explained for
    the configuration variable `core.quotePath` (see
    linkgit:git-config[1]).

4.  All the `file1` files in the output refer to files before the
    commit, and all the `file2` files refer to files after the commit.
    It is incorrect to apply each change to each file sequentially.  For
    example, this patch will swap a and b:

      diff --git a/a b/b
      rename from a
      rename to b
      diff --git a/b b/a
      rename from b
      rename to a

5.  Hunk headers mention the name of the function to which the hunk
    applies.  See "Defining a custom hunk-header" in
    linkgit:gitattributes[5] for details of how to tailor this to
    specific languages.


Combined diff format
--------------------

Any diff-generating command can take the `-c` or `--cc` option to
produce a 'combined diff' when showing a merge. This is the default
format when showing merges with linkgit:git-diff[1] or
linkgit:git-show[1]. Note also that you can give suitable
`--diff-merges` option to any of these commands to force generation of
diffs in a specific format.

A "combined diff" format looks like this:

------------
diff --combined describe.c
index fabadb8,cc95eb0..4866510
--- a/describe.c
+++ b/describe.c
@@@ -98,20 -98,12 +98,20 @@@
	return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
  }

- static void describe(char *arg)
 -static void describe(struct commit *cmit, int last_one)
++static void describe(char *arg, int last_one)
  {
 +	unsigned

Title: Generating Patch Text with Git
Summary
This section describes how Git generates patch text using the `-p` option with various commands, including the format of the patch text and how it can be customized, as well as the combined diff format for showing merges.