GIT-DIFF(1) | Git Manual | GIT-DIFF(1) |
NAME¶
git-diff - Show changes between commits, commit and working tree, etc
SYNOPSIS¶
git diff [<common diff options>] <commit>{0,2} [--] [<path>...]
DESCRIPTION¶
Show changes between two trees, a tree and the working tree, a tree and the index file, or the index file and the working tree.
git diff [--options] [--] [<path>...]
If exactly two paths are given, and at least one is untracked, compare the two files / directories. This behavior can be forced by --no-index.
git diff [--options] --cached [<commit>] [--] [<path>...]
git diff [--options] <commit> [--] [<path>...]
git diff [--options] <commit> <commit> [--] [<path>...]
git diff [--options] <commit>..<commit> [--] [<path>...]
git diff [--options] <commit>...<commit> [--] [<path>...]
Just in case if you are doing something exotic, it should be noted that all of the <commit> in the above description, except for the last two forms that use ".." notations, can be any <tree-ish>.
For a more complete list of ways to spell <commit>, see "SPECIFYING REVISIONS" section in git-rev-parse(1). However, "diff" is about comparing two endpoints, not ranges, and the range notations ("<commit>..<commit>" and "<commit>...<commit>") do not mean a range as defined in the "SPECIFYING RANGES" section in git-rev-parse(1).
OPTIONS¶
-p, -u
-U<n>, --unified=<n>
--raw
--patch-with-raw
--patience
--stat[=width[,name-width]]
--numstat
--shortstat
--dirstat[=limit]
--dirstat-by-file[=limit]
--summary
--patch-with-stat
-z
Without this option, each pathname output will have TAB, LF, double quotes, and backslash characters replaced with \t, \n, \", and \\, respectively, and the pathname will be enclosed in double quotes if any of those replacements occurred.
--name-only
--name-status
--submodule[=<format>]
--color[=<when>]
--no-color
--color-words[=<regex>]
When a <regex> is specified, every non-overlapping match of the <regex> is considered a word. Anything between these matches is considered whitespace and ignored(!) for the purposes of finding differences. You may want to append |[^[:space:]] to your regular expression to make sure that it matches all non-whitespace characters. A match that contains a newline is silently truncated(!) at the newline.
The regex can also be set via a diff driver or configuration option, see gitattributes(1) or git-config(1). Giving it explicitly overrides any diff driver or configuration setting. Diff drivers override configuration settings.
--no-renames
--check
--full-index
--binary
--abbrev[=<n>]
-B
-M
-C
--diff-filter=[ACDMRTUXB*]
--find-copies-harder
-l<num>
-S<string>
--pickaxe-all
--pickaxe-regex
-O<orderfile>
-R
--relative[=<path>]
-a, --text
--ignore-space-at-eol
-b, --ignore-space-change
-w, --ignore-all-space
--inter-hunk-context=<lines>
--exit-code
--quiet
--ext-diff
--no-ext-diff
--ignore-submodules
--src-prefix=<prefix>
--dst-prefix=<prefix>
--no-prefix
For more detailed explanation on these common options, see also gitdiffcore(7).
<path>...
RAW OUTPUT FORMAT¶
The raw output format from "git-diff-index", "git-diff-tree", "git-diff-files" and "git diff --raw" are very similar.
These commands all compare two sets of things; what is compared differs:
git-diff-index <tree-ish>
git-diff-index --cached <tree-ish>
git-diff-tree [-r] <tree-ish-1> <tree-ish-2> [<pattern>...]
git-diff-files [<pattern>...]
The "git-diff-tree" command begins its output by printing the hash of what is being compared. After that, all the commands print one output line per changed file.
An output line is formatted this way:
in-place edit :100644 100644 bcd1234... 0123456... M file0 copy-edit :100644 100644 abcd123... 1234567... C68 file1 file2 rename-edit :100644 100644 abcd123... 1234567... R86 file1 file3 create :000000 100644 0000000... 1234567... A file4 delete :100644 000000 1234567... 0000000... D file5 unmerged :000000 000000 0000000... 0000000... U file6
That is, from the left to the right:
Possible status letters are:
Status letters C and R are always followed by a score (denoting the percentage of similarity between the source and target of the move or copy), and are the only ones to be so.
<sha1> is shown as all 0’s if a file is new on the filesystem and it is out of sync with the index.
Example:
:100644 100644 5be4a4...... 000000...... M file.c
When -z option is not used, TAB, LF, and backslash characters in pathnames are represented as \t, \n, and \\, respectively.
DIFF FORMAT FOR MERGES¶
"git-diff-tree", "git-diff-files" and "git-diff --raw" can take -c or --cc option to generate diff output also for merge commits. The output differs from the format described above in the following way:
Example:
::100644 100644 100644 fabadb8... cc95eb0... 4866510... MM describe.c
Note that combined diff lists only files which were modified from all parents.
GENERATING PATCHES WITH -P¶
When "git-diff-index", "git-diff-tree", or "git-diff-files" are run with a -p option, "git diff" without the --raw option, or "git log" with the "-p" option, they do not produce the output described above; instead they produce a patch file. You can customize the creation of such patches via the GIT_EXTERNAL_DIFF and the GIT_DIFF_OPTS environment variables.
What the -p option produces is slightly different from the traditional diff format.
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 a/ or b/ filenames.
When 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 rename/copy produces, respectively.
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>
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.
COMBINED DIFF FORMAT¶
"git-diff-tree", "git-diff-files" and "git-diff" can take -c or --cc option to produce combined diff. For showing a merge commit with "git log -p", this is the default format; you can force showing full diff with the -m option. 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 char sha1[20];
+ struct commit *cmit;
struct commit_list *list;
static int initialized = 0;
struct commit_name *n;
+ if (get_sha1(arg, sha1) < 0)
+ usage(describe_usage);
+ cmit = lookup_commit_reference(sha1);
+ if (!cmit)
+ usage(describe_usage);
+
if (!initialized) {
initialized = 1;
for_each_ref(get_name);
diff --combined file
or like this (when --cc option is used):
diff --cc file
index <hash>,<hash>..<hash> mode <mode>,<mode>..<mode> new file mode <mode> deleted file mode <mode>,<mode>
The mode <mode>,<mode>..<mode> line appears only if at least one of the <mode> is different from the rest. Extended headers with information about detected contents movement (renames and copying detection) are designed to work with diff of two <tree-ish> and are not used by combined diff format.
--- a/file +++ b/file
Similar to two-line header for traditional unified diff format, /dev/null is used to signal created or deleted files.
@@@ <from-file-range> <from-file-range> <to-file-range> @@@
There are (number of parents + 1) @ characters in the chunk header for combined diff format.
Unlike the traditional unified diff format, which shows two files A and B with a single column that has - (minus — appears in A but removed in B), + (plus — missing in A but added to B), or " " (space — unchanged) prefix, this format compares two or more files file1, file2,... with one file X, and shows how X differs from each of fileN. One column for each of fileN is prepended to the output line to note how X’s line is different from it.
A - character in the column N means that the line appears in fileN but it does not appear in the result. A + character in the column N means that the line appears in the result, and fileN does not have that line (in other words, the line was added, from the point of view of that parent).
In the above example output, the function signature was changed from both files (hence two - removals from both file1 and file2, plus ++ to mean one line that was added does not appear in either file1 nor file2). Also eight other lines are the same from file1 but do not appear in file2 (hence prefixed with +).
When shown by git diff-tree -c, it compares the parents of a merge commit with the merge result (i.e. file1..fileN are the parents). When shown by git diff-files -c, it compares the two unresolved merge parents with the working tree file (i.e. file1 is stage 2 aka "our version", file2 is stage 3 aka "their version").
OTHER DIFF FORMATS¶
The --summary option describes newly added, deleted, renamed and copied files. The --stat option adds diffstat(1) graph to the output. These options can be combined with other options, such as -p, and are meant for human consumption.
When showing a change that involves a rename or a copy, --stat output formats the pathnames compactly by combining common prefix and suffix of the pathnames. For example, a change that moves arch/i386/Makefile to arch/x86/Makefile while modifying 4 lines will be shown like this:
arch/{i386 => x86}/Makefile | 4 +--
The --numstat option gives the diffstat(1) information but is designed for easier machine consumption. An entry in --numstat output looks like this:
1 2 README 3 1 arch/{i386 => x86}/Makefile
That is, from left to right:
When -z output option is in effect, the output is formatted this way:
1 2 README NUL 3 1 NUL arch/i386/Makefile NUL arch/x86/Makefile NUL
That is:
The extra NUL before the preimage path in renamed case is to allow scripts that read the output to tell if the current record being read is a single-path record or a rename/copy record without reading ahead. After reading added and deleted lines, reading up to NUL would yield the pathname, but if that is NUL, the record will show two paths.
EXAMPLES¶
Various ways to check your working tree
$ git diff (1) $ git diff --cached (2) $ git diff HEAD (3)
1. Changes in the working tree not yet staged for the next
commit.
2. Changes between the index and your last commit; what you would be
committing if you run "git commit" without "-a" option.
3. Changes in the working tree since your last commit; what you would
be committing if you run "git commit -a"
Comparing with arbitrary commits
$ git diff test (1) $ git diff HEAD -- ./test (2) $ git diff HEAD^ HEAD (3)
1. Instead of using the tip of the current branch, compare
with the tip of "test" branch.
2. Instead of comparing with the tip of "test" branch,
compare with the tip of the current branch, but limit the comparison to the
file "test".
3. Compare the version before the last commit and the last commit.
Comparing branches
$ git diff topic master (1) $ git diff topic..master (2) $ git diff topic...master (3)
1. Changes between the tips of the topic and the master
branches.
2. Same as above.
3. Changes that occurred on the master branch since when the topic
branch was started off it.
Limiting the diff output
$ git diff --diff-filter=MRC (1) $ git diff --name-status (2) $ git diff arch/i386 include/asm-i386 (3)
1. Show only modification, rename and copy, but not
addition nor deletion.
2. Show only names and the nature of change, but not actual diff
output.
3. Limit diff output to named subtrees.
Munging the diff output
$ git diff --find-copies-harder -B -C (1) $ git diff -R (2)
1. Spend extra cycles to find renames, copies and complete
rewrites (very expensive).
2. Output diff in reverse.
SEE ALSO¶
AUTHOR¶
Written by Linus Torvalds <torvalds@osdl.org[1]>
DOCUMENTATION¶
Documentation by Junio C Hamano and the git-list <git@vger.kernel.org[2]>.
GIT¶
Part of the git(1) suite
NOTES¶
- 1.
- torvalds@osdl.org
- 2.
- git@vger.kernel.org
02/03/2020 | Git 1.7.1 |