The basic usage of git diff
which, in general, compares two tree-ish objects, where a tree-ish object is one of:
git diff [<options>] [<commit>] [--] [<path>...] git diff [<options>] --cached [<commit>] [--] [<path>...] git diff [<options>] <commit> <commit> [--] [<path>...] git diff [<options>] <blob> <blob> git diff [<options>] --no-index [--] <path> <path>
Default output format is the patch-style difference to change from the first argument to the second argument:
$ git diff diff --git a/Makefile b/Makefile index 96c5335e7ee4..71913214b18c 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +junk here + # SPDX-License-Identifier: GPL-2.0 VERSION = 5 PATCHLEVEL = 0 $
The tree-ish objects in play:
Most common comparisons:
$ git diff [working tree vs index] $ git diff --cached/--staged [index vs HEAD] $ git diff HEAD [working tree vs HEAD]
With one argument, compare working tree to that argument:
$ git diff HEAD [working tree vs HEAD, as above] $ git diff deadbeef [working tree versus commit deadbeef] $ git diff bugfix42 [working tree versus branch bugfix42] $ git diff v1.0 [working tree versus tag v1.0]
With –cached
(or --staged
), as above but with index:
$ git diff --cached HEAD $ git diff --cached deadbeef $ git diff --cached bugfix42 $ git diff --cached v1.0
Comparing two explicit commits in various ways:
$ git diff v1.0 v2.0 $ git diff master bugfix42 $ git diff HEAD^ HEAD $ git diff deadbeef master
Compare only part of the tree-ish objects:
$ git diff v4.19 v4.20 -- scripts/dtc/ diff --git a/scripts/dtc/Makefile b/scripts/dtc/Makefile index 1c943e03eaf2..056d5da6c477 100644 --- a/scripts/dtc/Makefile +++ b/scripts/dtc/Makefile @@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 # scripts/dtc makefile -hostprogs-y := dtc +hostprogs-$(CONFIG_DTC) := dtc always := $(hostprogs-y) dtc-objs := dtc.o flattree.o fstree.o data.o livetree.o treesource.o \ ... lots of output snipped ...
Sometimes you don't need to see all of the output:
$ git diff v4.19 v4.20 -- scripts/dtc/ | wc -l 2097 $
Normalized histogram:
$ git diff v4.19 v4.20 --stat -- scripts/ scripts/dtc/checks.c | 291 +++++++++++++++++++++++- scripts/dtc/dtc-lexer.lex.c_shipped | 10 +- scripts/dtc/dtc-parser.tab.c_shipped | 430 ++++++++++++++++++----------------- scripts/dtc/dtc-parser.y | 20 +- scripts/dtc/dtc.c | 2 +- scripts/dtc/dtc.h | 3 + scripts/dtc/libfdt/fdt_addresses.c | 96 ++++++++ scripts/dtc/libfdt/fdt_empty_tree.c | 1 - scripts/dtc/libfdt/fdt_overlay.c | 861 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ scripts/dtc/libfdt/fdt_ro.c | 4 +- scripts/dtc/libfdt/fdt_rw.c | 24 +- scripts/dtc/libfdt/fdt_sw.c | 16 +- scripts/dtc/libfdt/fdt_wip.c | 4 +- scripts/dtc/libfdt/libfdt.h | 47 ++++ scripts/dtc/livetree.c | 31 ++- scripts/dtc/update-dtc-source.sh | 4 +- scripts/dtc/version_gen.h | 2 +- 17 files changed, 1615 insertions(+), 231 deletions(-) $
$ git diff v4.14 v4.15 --name-only -- scripts/dtc/ scripts/dtc/checks.c scripts/dtc/dtc-lexer.lex.c_shipped scripts/dtc/dtc-parser.tab.c_shipped scripts/dtc/dtc-parser.y scripts/dtc/dtc.c scripts/dtc/dtc.h scripts/dtc/libfdt/fdt_addresses.c scripts/dtc/libfdt/fdt_empty_tree.c scripts/dtc/libfdt/fdt_overlay.c scripts/dtc/libfdt/fdt_ro.c scripts/dtc/libfdt/fdt_rw.c scripts/dtc/libfdt/fdt_sw.c scripts/dtc/libfdt/fdt_wip.c scripts/dtc/libfdt/libfdt.h scripts/dtc/livetree.c scripts/dtc/update-dtc-source.sh scripts/dtc/version_gen.h $
General form:
$ git diff <commit1>:<file2> <commit2>:<file2>
For example, after a rename:
$ git diff v4.18:Makefile v4.19:makefile
git diff [<options>] <commit> <commit> [--] [<path>...] This is to view the changes between two arbitrary <commit>. git diff [<options>] <commit>..<commit> [--] [<path>...] This is synonymous to the previous form. If <commit> on one side is omitted, it will have the same effect as using HEAD instead. git diff [<options>] <commit>...<commit> [--] [<path>...] This form is to view the changes on the branch containing and up to the second <commit>, starting at a common ancestor of both <commit>. "git diff A...B" is equivalent to "git diff $(git merge-base A B) B". You can omit any one of <commit>, which has the same effect as using HEAD instead.
$ git show fix_readme commit 5f1992a4245c301ed5e556f4ef47153981a67820 (fix_readme) Author: Robert P. J. Day <rpjday@crashcourse.ca> Date: Fri Mar 1 05:27:13 2019 -0500 fix readme diff --git a/README.asc b/README.asc index fa40bad..b7f3816 100644 --- a/README.asc +++ b/README.asc @@ -1,3 +1,4 @@ +stuff in README.asc = Pro Git, Second Edition Welcome to the second edition of the Pro Git book.
$ git show fix_license commit 65633ac0f606e70db50460502989a7a74dca8e20 (fix_license) Author: Robert P. J. Day <rpjday@crashcourse.ca> Date: Fri Mar 1 05:26:23 2019 -0500 fix license diff --git a/LICENSE.asc b/LICENSE.asc index 81f2824..19f935b 100644 --- a/LICENSE.asc +++ b/LICENSE.asc @@ -1 +1,2 @@ +stuff in LICENSE.asc This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
$ git diff fix_readme fix_license diff --git a/LICENSE.asc b/LICENSE.asc index 81f2824..19f935b 100644 --- a/LICENSE.asc +++ b/LICENSE.asc @@ -1 +1,2 @@ +stuff in LICENSE.asc This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. diff --git a/README.asc b/README.asc index b7f3816..fa40bad 100644 --- a/README.asc +++ b/README.asc @@ -1,4 +1,3 @@ -stuff in README.asc = Pro Git, Second Edition Welcome to the second edition of the Pro Git book.
Using ..
means exactly the same thing:
$ git diff fix_readme..fix_license diff --git a/LICENSE.asc b/LICENSE.asc index 81f2824..19f935b 100644 --- a/LICENSE.asc +++ b/LICENSE.asc @@ -1 +1,2 @@ +stuff in LICENSE.asc This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. diff --git a/README.asc b/README.asc index b7f3816..fa40bad 100644 --- a/README.asc +++ b/README.asc @@ -1,4 +1,3 @@ -stuff in README.asc = Pro Git, Second Edition Welcome to the second edition of the Pro Git book.
However, using ...
means something different:
$ git diff fix_license...fix_readme diff --git a/README.asc b/README.asc index fa40bad..b7f3816 100644 --- a/README.asc +++ b/README.asc @@ -1,3 +1,4 @@ +stuff in README.asc = Pro Git, Second Edition Welcome to the second edition of the Pro Git book.
Just scratching the surface:
--ignore-cr-at-eol Ignore carrige-return at the end of line when doing a comparison. --ignore-space-at-eol Ignore changes in whitespace at EOL. -b, --ignore-space-change Ignore changes in amount of whitespace. This ignores whitespace at line end, and considers all other sequences of one or more whitespace characters to be equivalent. -w, --ignore-all-space Ignore whitespace when comparing lines. This ignores differences even if one line has whitespace where the other line has none. --ignore-blank-lines Ignore changes whose lines are all blank.