1GIT-DIFF(1)                       Git Manual                       GIT-DIFF(1)
2
3
4

NAME

6       git-diff - Show changes between commits, commit and working tree, etc
7

SYNOPSIS

9       git diff [<common diff options>] <commit>{0,2} [--] [<path>...]
10

DESCRIPTION

12       Show changes between two trees, a tree and the working tree, a tree and
13       the index file, or the index file and the working tree.
14
15       git diff [--options] [--] [<path>...]
16           This form is to view the changes you made relative to the index
17           (staging area for the next commit). In other words, the differences
18           are what you could tell git to further add to the index but you
19           still haven’t. You can stage these changes by using git-add(1).
20
21           If exactly two paths are given, and at least one is untracked,
22           compare the two files / directories. This behavior can be forced by
23           --no-index.
24
25       git diff [--options] --cached [<commit>] [--] [<path>...]
26           This form is to view the changes you staged for the next commit
27           relative to the named <commit>. Typically you would want comparison
28           with the latest commit, so if you do not give <commit>, it defaults
29           to HEAD. --staged is a synonym of --cached.
30
31       git diff [--options] <commit> [--] [<path>...]
32           This form is to view the changes you have in your working tree
33           relative to the named <commit>. You can use HEAD to compare it with
34           the latest commit, or a branch name to compare with the tip of a
35           different branch.
36
37       git diff [--options] <commit> <commit> [--] [<path>...]
38           This is to view the changes between two arbitrary <commit>.
39
40       git diff [--options] <commit>..<commit> [--] [<path>...]
41           This is synonymous to the previous form. If <commit> on one side is
42           omitted, it will have the same effect as using HEAD instead.
43
44       git diff [--options] <commit>...<commit> [--] [<path>...]
45           This form is to view the changes on the branch containing and up to
46           the second <commit>, starting at a common ancestor of both
47           <commit>. "git diff A...B" is equivalent to "git diff
48           $(git-merge-base A B) B". You can omit any one of <commit>, which
49           has the same effect as using HEAD instead.
50
51       Just in case if you are doing something exotic, it should be noted that
52       all of the <commit> in the above description, except for the last two
53       forms that use ".." notations, can be any <tree-ish>.
54
55       For a more complete list of ways to spell <commit>, see "SPECIFYING
56       REVISIONS" section in git-rev-parse(1). However, "diff" is about
57       comparing two endpoints, not ranges, and the range notations
58       ("<commit>..<commit>" and "<commit>...<commit>") do not mean a range as
59       defined in the "SPECIFYING RANGES" section in git-rev-parse(1).
60

OPTIONS

62       -p, -u
63           Generate patch (see section on generating patches). This is the
64           default.
65
66       -U<n>, --unified=<n>
67           Generate diffs with <n> lines of context instead of the usual
68           three. Implies -p.
69
70       --raw
71           Generate the raw format.
72
73       --patch-with-raw
74           Synonym for -p --raw.
75
76       --patience
77           Generate a diff using the "patience diff" algorithm.
78
79       --stat[=width[,name-width]]
80           Generate a diffstat. You can override the default output width for
81           80-column terminal by --stat=width. The width of the filename part
82           can be controlled by giving another width to it separated by a
83           comma.
84
85       --numstat
86           Similar to --stat, but shows number of added and deleted lines in
87           decimal notation and pathname without abbreviation, to make it more
88           machine friendly. For binary files, outputs two - instead of saying
89           0 0.
90
91       --shortstat
92           Output only the last line of the --stat format containing total
93           number of modified files, as well as number of added and deleted
94           lines.
95
96       --dirstat[=limit]
97           Output the distribution of relative amount of changes (number of
98           lines added or removed) for each sub-directory. Directories with
99           changes below a cut-off percent (3% by default) are not shown. The
100           cut-off percent can be set with --dirstat=limit. Changes in a child
101           directory is not counted for the parent directory, unless
102           --cumulative is used.
103
104       --dirstat-by-file[=limit]
105           Same as --dirstat, but counts changed files instead of lines.
106
107       --summary
108           Output a condensed summary of extended header information such as
109           creations, renames and mode changes.
110
111       --patch-with-stat
112           Synonym for -p --stat.
113
114       -z
115           When --raw, --numstat, --name-only or --name-status has been given,
116           do not munge pathnames and use NULs as output field terminators.
117
118           Without this option, each pathname output will have TAB, LF, double
119           quotes, and backslash characters replaced with \t, \n, \", and \\,
120           respectively, and the pathname will be enclosed in double quotes if
121           any of those replacements occurred.
122
123       --name-only
124           Show only names of changed files.
125
126       --name-status
127           Show only names and status of changed files. See the description of
128           the --diff-filter option on what the status letters mean.
129
130       --submodule[=<format>]
131           Chose the output format for submodule differences. <format> can be
132           one of short and log.  short just shows pairs of commit names, this
133           format is used when this option is not given.  log is the default
134           value for this option and lists the commits in that commit range
135           like the summary option of git-submodule(1) does.
136
137       --color[=<when>]
138           Show colored diff. The value must be always (the default), never,
139           or auto.
140
141       --no-color
142           Turn off colored diff, even when the configuration file gives the
143           default to color output. Same as --color=never.
144
145       --color-words[=<regex>]
146           Show colored word diff, i.e., color words which have changed. By
147           default, words are separated by whitespace.
148
149           When a <regex> is specified, every non-overlapping match of the
150           <regex> is considered a word. Anything between these matches is
151           considered whitespace and ignored(!) for the purposes of finding
152           differences. You may want to append |[^[:space:]] to your regular
153           expression to make sure that it matches all non-whitespace
154           characters. A match that contains a newline is silently
155           truncated(!) at the newline.
156
157           The regex can also be set via a diff driver or configuration
158           option, see gitattributes(1) or git-config(1). Giving it explicitly
159           overrides any diff driver or configuration setting. Diff drivers
160           override configuration settings.
161
162       --no-renames
163           Turn off rename detection, even when the configuration file gives
164           the default to do so.
165
166       --check
167           Warn if changes introduce trailing whitespace or an indent that
168           uses a space before a tab. Exits with non-zero status if problems
169           are found. Not compatible with --exit-code.
170
171       --full-index
172           Instead of the first handful of characters, show the full pre- and
173           post-image blob object names on the "index" line when generating
174           patch format output.
175
176       --binary
177           In addition to --full-index, output a binary diff that can be
178           applied with git-apply.
179
180       --abbrev[=<n>]
181           Instead of showing the full 40-byte hexadecimal object name in
182           diff-raw format output and diff-tree header lines, show only a
183           partial prefix. This is independent of the --full-index option
184           above, which controls the diff-patch output format. Non default
185           number of digits can be specified with --abbrev=<n>.
186
187       -B
188           Break complete rewrite changes into pairs of delete and create.
189
190       -M
191           Detect renames.
192
193       -C
194           Detect copies as well as renames. See also --find-copies-harder.
195
196       --diff-filter=[ACDMRTUXB*]
197           Select only files that are Added (A), Copied (C), Deleted (D),
198           Modified (M), Renamed (R), have their type (i.e. regular file,
199           symlink, submodule, ...) changed (T), are Unmerged (U), are Unknown
200           (X), or have had their pairing Broken (B). Any combination of the
201           filter characters may be used. When * (All-or-none) is added to the
202           combination, all paths are selected if there is any file that
203           matches other criteria in the comparison; if there is no file that
204           matches other criteria, nothing is selected.
205
206       --find-copies-harder
207           For performance reasons, by default, -C option finds copies only if
208           the original file of the copy was modified in the same changeset.
209           This flag makes the command inspect unmodified files as candidates
210           for the source of copy. This is a very expensive operation for
211           large projects, so use it with caution. Giving more than one -C
212           option has the same effect.
213
214       -l<num>
215           The -M and -C options require O(n^2) processing time where n is the
216           number of potential rename/copy targets. This option prevents
217           rename/copy detection from running if the number of rename/copy
218           targets exceeds the specified number.
219
220       -S<string>
221           Look for differences that introduce or remove an instance of
222           <string>. Note that this is different than the string simply
223           appearing in diff output; see the pickaxe entry in gitdiffcore(7)
224           for more details.
225
226       --pickaxe-all
227           When -S finds a change, show all the changes in that changeset, not
228           just the files that contain the change in <string>.
229
230       --pickaxe-regex
231           Make the <string> not a plain string but an extended POSIX regex to
232           match.
233
234       -O<orderfile>
235           Output the patch in the order specified in the <orderfile>, which
236           has one shell glob pattern per line.
237
238       -R
239           Swap two inputs; that is, show differences from index or on-disk
240           file to tree contents.
241
242       --relative[=<path>]
243           When run from a subdirectory of the project, it can be told to
244           exclude changes outside the directory and show pathnames relative
245           to it with this option. When you are not in a subdirectory (e.g. in
246           a bare repository), you can name which subdirectory to make the
247           output relative to by giving a <path> as an argument.
248
249       -a, --text
250           Treat all files as text.
251
252       --ignore-space-at-eol
253           Ignore changes in whitespace at EOL.
254
255       -b, --ignore-space-change
256           Ignore changes in amount of whitespace. This ignores whitespace at
257           line end, and considers all other sequences of one or more
258           whitespace characters to be equivalent.
259
260       -w, --ignore-all-space
261           Ignore whitespace when comparing lines. This ignores differences
262           even if one line has whitespace where the other line has none.
263
264       --inter-hunk-context=<lines>
265           Show the context between diff hunks, up to the specified number of
266           lines, thereby fusing hunks that are close to each other.
267
268       --exit-code
269           Make the program exit with codes similar to diff(1). That is, it
270           exits with 1 if there were differences and 0 means no differences.
271
272       --quiet
273           Disable all output of the program. Implies --exit-code.
274
275       --ext-diff
276           Allow an external diff helper to be executed. If you set an
277           external diff driver with gitattributes(5), you need to use this
278           option with git-log(1) and friends.
279
280       --no-ext-diff
281           Disallow external diff drivers.
282
283       --ignore-submodules
284           Ignore changes to submodules in the diff generation.
285
286       --src-prefix=<prefix>
287           Show the given source prefix instead of "a/".
288
289       --dst-prefix=<prefix>
290           Show the given destination prefix instead of "b/".
291
292       --no-prefix
293           Do not show any source or destination prefix.
294
295       For more detailed explanation on these common options, see also
296       gitdiffcore(7).
297
298       <path>...
299           The <paths> parameters, when given, are used to limit the diff to
300           the named paths (you can give directory names and get diff for all
301           files under them).
302

RAW OUTPUT FORMAT

304       The raw output format from "git-diff-index", "git-diff-tree",
305       "git-diff-files" and "git diff --raw" are very similar.
306
307       These commands all compare two sets of things; what is compared
308       differs:
309
310       git-diff-index <tree-ish>
311           compares the <tree-ish> and the files on the filesystem.
312
313       git-diff-index --cached <tree-ish>
314           compares the <tree-ish> and the index.
315
316       git-diff-tree [-r] <tree-ish-1> <tree-ish-2> [<pattern>...]
317           compares the trees named by the two arguments.
318
319       git-diff-files [<pattern>...]
320           compares the index and the files on the filesystem.
321
322       The "git-diff-tree" command begins its output by printing the hash of
323       what is being compared. After that, all the commands print one output
324       line per changed file.
325
326       An output line is formatted this way:
327
328           in-place edit  :100644 100644 bcd1234... 0123456... M file0
329           copy-edit      :100644 100644 abcd123... 1234567... C68 file1 file2
330           rename-edit    :100644 100644 abcd123... 1234567... R86 file1 file3
331           create         :000000 100644 0000000... 1234567... A file4
332           delete         :100644 000000 1234567... 0000000... D file5
333           unmerged       :000000 000000 0000000... 0000000... U file6
334
335
336       That is, from the left to the right:
337
338        1. a colon.
339
340        2. mode for "src"; 000000 if creation or unmerged.
341
342        3. a space.
343
344        4. mode for "dst"; 000000 if deletion or unmerged.
345
346        5. a space.
347
348        6. sha1 for "src"; 0{40} if creation or unmerged.
349
350        7. a space.
351
352        8. sha1 for "dst"; 0{40} if creation, unmerged or "look at work tree".
353
354        9. a space.
355
356       10. status, followed by optional "score" number.
357
358       11. a tab or a NUL when -z option is used.
359
360       12. path for "src"
361
362       13. a tab or a NUL when -z option is used; only exists for C or R.
363
364       14. path for "dst"; only exists for C or R.
365
366       15. an LF or a NUL when -z option is used, to terminate the record.
367
368       Possible status letters are:
369
370       ·   A: addition of a file
371
372       ·   C: copy of a file into a new one
373
374       ·   D: deletion of a file
375
376       ·   M: modification of the contents or mode of a file
377
378       ·   R: renaming of a file
379
380       ·   T: change in the type of the file
381
382       ·   U: file is unmerged (you must complete the merge before it can be
383           committed)
384
385       ·   X: "unknown" change type (most probably a bug, please report it)
386
387       Status letters C and R are always followed by a score (denoting the
388       percentage of similarity between the source and target of the move or
389       copy), and are the only ones to be so.
390
391       <sha1> is shown as all 0’s if a file is new on the filesystem and it is
392       out of sync with the index.
393
394       Example:
395
396           :100644 100644 5be4a4...... 000000...... M file.c
397
398
399       When -z option is not used, TAB, LF, and backslash characters in
400       pathnames are represented as \t, \n, and \\, respectively.
401

DIFF FORMAT FOR MERGES

403       "git-diff-tree", "git-diff-files" and "git-diff --raw" can take -c or
404       --cc option to generate diff output also for merge commits. The output
405       differs from the format described above in the following way:
406
407        1. there is a colon for each parent
408
409        2. there are more "src" modes and "src" sha1
410
411        3. status is concatenated status characters for each parent
412
413        4. no optional "score" number
414
415        5. single path, only for "dst"
416
417       Example:
418
419           ::100644 100644 100644 fabadb8... cc95eb0... 4866510... MM      describe.c
420
421
422       Note that combined diff lists only files which were modified from all
423       parents.
424

GENERATING PATCHES WITH -P

426       When "git-diff-index", "git-diff-tree", or "git-diff-files" are run
427       with a -p option, "git diff" without the --raw option, or "git log"
428       with the "-p" option, they do not produce the output described above;
429       instead they produce a patch file. You can customize the creation of
430       such patches via the GIT_EXTERNAL_DIFF and the GIT_DIFF_OPTS
431       environment variables.
432
433       What the -p option produces is slightly different from the traditional
434       diff format.
435
436        1. It is preceded with a "git diff" header, that looks like this:
437
438               diff --git a/file1 b/file2
439
440           The a/ and b/ filenames are the same unless rename/copy is
441           involved. Especially, even for a creation or a deletion, /dev/null
442           is not used in place of a/ or b/ filenames.
443
444           When rename/copy is involved, file1 and file2 show the name of the
445           source file of the rename/copy and the name of the file that
446           rename/copy produces, respectively.
447
448        2. It is followed by one or more extended header lines:
449
450               old mode <mode>
451               new mode <mode>
452               deleted file mode <mode>
453               new file mode <mode>
454               copy from <path>
455               copy to <path>
456               rename from <path>
457               rename to <path>
458               similarity index <number>
459               dissimilarity index <number>
460               index <hash>..<hash> <mode>
461
462        3. TAB, LF, double quote and backslash characters in pathnames are
463           represented as \t, \n, \" and \\, respectively. If there is need
464           for such substitution then the whole pathname is put in double
465           quotes.
466
467       The similarity index is the percentage of unchanged lines, and the
468       dissimilarity index is the percentage of changed lines. It is a rounded
469       down integer, followed by a percent sign. The similarity index value of
470       100% is thus reserved for two equal files, while 100% dissimilarity
471       means that no line from the old file made it into the new one.
472

COMBINED DIFF FORMAT

474       "git-diff-tree", "git-diff-files" and "git-diff" can take -c or --cc
475       option to produce combined diff. For showing a merge commit with "git
476       log -p", this is the default format; you can force showing full diff
477       with the -m option. A combined diff format looks like this:
478
479           diff --combined describe.c
480           index fabadb8,cc95eb0..4866510
481           --- a/describe.c
482           +++ b/describe.c
483           @@@ -98,20 -98,12 +98,20 @@@
484                   return (a_date > b_date) ? -1 : (a_date == b_date) ? 0 : 1;
485             }
486
487           - static void describe(char *arg)
488            -static void describe(struct commit *cmit, int last_one)
489           ++static void describe(char *arg, int last_one)
490             {
491            +      unsigned char sha1[20];
492            +      struct commit *cmit;
493                   struct commit_list *list;
494                   static int initialized = 0;
495                   struct commit_name *n;
496
497            +      if (get_sha1(arg, sha1) < 0)
498            +              usage(describe_usage);
499            +      cmit = lookup_commit_reference(sha1);
500            +      if (!cmit)
501            +              usage(describe_usage);
502            +
503                   if (!initialized) {
504                           initialized = 1;
505                           for_each_ref(get_name);
506
507
508
509        1. It is preceded with a "git diff" header, that looks like this (when
510           -c option is used):
511
512               diff --combined file
513
514           or like this (when --cc option is used):
515
516               diff --cc file
517
518        2. It is followed by one or more extended header lines (this example
519           shows a merge with two parents):
520
521               index <hash>,<hash>..<hash>
522               mode <mode>,<mode>..<mode>
523               new file mode <mode>
524               deleted file mode <mode>,<mode>
525
526           The mode <mode>,<mode>..<mode> line appears only if at least one of
527           the <mode> is different from the rest. Extended headers with
528           information about detected contents movement (renames and copying
529           detection) are designed to work with diff of two <tree-ish> and are
530           not used by combined diff format.
531
532        3. It is followed by two-line from-file/to-file header
533
534               --- a/file
535               +++ b/file
536
537           Similar to two-line header for traditional unified diff format,
538           /dev/null is used to signal created or deleted files.
539
540        4. Chunk header format is modified to prevent people from accidentally
541           feeding it to patch -p1. Combined diff format was created for
542           review of merge commit changes, and was not meant for apply. The
543           change is similar to the change in the extended index header:
544
545               @@@ <from-file-range> <from-file-range> <to-file-range> @@@
546
547           There are (number of parents + 1) @ characters in the chunk header
548           for combined diff format.
549
550       Unlike the traditional unified diff format, which shows two files A and
551       B with a single column that has - (minus — appears in A but removed in
552       B), + (plus — missing in A but added to B), or " " (space — unchanged)
553       prefix, this format compares two or more files file1, file2,... with
554       one file X, and shows how X differs from each of fileN. One column for
555       each of fileN is prepended to the output line to note how X’s line is
556       different from it.
557
558       A - character in the column N means that the line appears in fileN but
559       it does not appear in the result. A + character in the column N means
560       that the line appears in the result, and fileN does not have that line
561       (in other words, the line was added, from the point of view of that
562       parent).
563
564       In the above example output, the function signature was changed from
565       both files (hence two - removals from both file1 and file2, plus ++ to
566       mean one line that was added does not appear in either file1 nor
567       file2). Also eight other lines are the same from file1 but do not
568       appear in file2 (hence prefixed with +).
569
570       When shown by git diff-tree -c, it compares the parents of a merge
571       commit with the merge result (i.e. file1..fileN are the parents). When
572       shown by git diff-files -c, it compares the two unresolved merge
573       parents with the working tree file (i.e. file1 is stage 2 aka "our
574       version", file2 is stage 3 aka "their version").
575

OTHER DIFF FORMATS

577       The --summary option describes newly added, deleted, renamed and copied
578       files. The --stat option adds diffstat(1) graph to the output. These
579       options can be combined with other options, such as -p, and are meant
580       for human consumption.
581
582       When showing a change that involves a rename or a copy, --stat output
583       formats the pathnames compactly by combining common prefix and suffix
584       of the pathnames. For example, a change that moves arch/i386/Makefile
585       to arch/x86/Makefile while modifying 4 lines will be shown like this:
586
587           arch/{i386 => x86}/Makefile    |   4 +--
588
589
590       The --numstat option gives the diffstat(1) information but is designed
591       for easier machine consumption. An entry in --numstat output looks like
592       this:
593
594           1       2       README
595           3       1       arch/{i386 => x86}/Makefile
596
597
598       That is, from left to right:
599
600        1. the number of added lines;
601
602        2. a tab;
603
604        3. the number of deleted lines;
605
606        4. a tab;
607
608        5. pathname (possibly with rename/copy information);
609
610        6. a newline.
611
612       When -z output option is in effect, the output is formatted this way:
613
614           1       2       README NUL
615           3       1       NUL arch/i386/Makefile NUL arch/x86/Makefile NUL
616
617
618       That is:
619
620        1. the number of added lines;
621
622        2. a tab;
623
624        3. the number of deleted lines;
625
626        4. a tab;
627
628        5. a NUL (only exists if renamed/copied);
629
630        6. pathname in preimage;
631
632        7. a NUL (only exists if renamed/copied);
633
634        8. pathname in postimage (only exists if renamed/copied);
635
636        9. a NUL.
637
638       The extra NUL before the preimage path in renamed case is to allow
639       scripts that read the output to tell if the current record being read
640       is a single-path record or a rename/copy record without reading ahead.
641       After reading added and deleted lines, reading up to NUL would yield
642       the pathname, but if that is NUL, the record will show two paths.
643

EXAMPLES

645       Various ways to check your working tree
646
647               $ git diff            (1)
648               $ git diff --cached   (2)
649               $ git diff HEAD       (3)
650
651           1. Changes in the working tree not yet staged for the next commit.
652           2. Changes between the index and your last commit; what you would
653           be committing if you run "git commit" without "-a" option.
654           3. Changes in the working tree since your last commit; what you
655           would be committing if you run "git commit -a"
656
657       Comparing with arbitrary commits
658
659               $ git diff test            (1)
660               $ git diff HEAD -- ./test  (2)
661               $ git diff HEAD^ HEAD      (3)
662
663           1. Instead of using the tip of the current branch, compare with the
664           tip of "test" branch.
665           2. Instead of comparing with the tip of "test" branch, compare with
666           the tip of the current branch, but limit the comparison to the file
667           "test".
668           3. Compare the version before the last commit and the last commit.
669
670       Comparing branches
671
672               $ git diff topic master    (1)
673               $ git diff topic..master   (2)
674               $ git diff topic...master  (3)
675
676           1. Changes between the tips of the topic and the master branches.
677           2. Same as above.
678           3. Changes that occurred on the master branch since when the topic
679           branch was started off it.
680
681       Limiting the diff output
682
683               $ git diff --diff-filter=MRC            (1)
684               $ git diff --name-status                (2)
685               $ git diff arch/i386 include/asm-i386   (3)
686
687           1. Show only modification, rename and copy, but not addition nor
688           deletion.
689           2. Show only names and the nature of change, but not actual diff
690           output.
691           3. Limit diff output to named subtrees.
692
693       Munging the diff output
694
695               $ git diff --find-copies-harder -B -C  (1)
696               $ git diff -R                          (2)
697
698           1. Spend extra cycles to find renames, copies and complete rewrites
699           (very expensive).
700           2. Output diff in reverse.
701

SEE ALSO

703       git-difftool(1)
704           Show changes using common diff tools
705

AUTHOR

707       Written by Linus Torvalds <torvalds@osdl.org[1]>
708

DOCUMENTATION

710       Documentation by Junio C Hamano and the git-list
711       <git@vger.kernel.org[2]>.
712

GIT

714       Part of the git(1) suite
715

NOTES

717        1. torvalds@osdl.org
718           mailto:torvalds@osdl.org
719
720        2. git@vger.kernel.org
721           mailto:git@vger.kernel.org
722
723
724
725Git 1.7.1                         08/16/2017                       GIT-DIFF(1)
Impressum