This is an old revision of the document!
Overview
The basic usage of git diff which, in general, compares two tree-ish objects, where a tree-ish object is one of:
- a specific commit (HEAD, SHA-1 hash, tag or branch name)
- the working tree
- the index
SYNOPSIS
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>
Output of "git diff"
Default output format is the patch-style difference to change from the first argument to the second arguent:
$ 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 $
Basic "git diff"
The tree-ish objects in play:
- HEAD: The most recent commit on the current branch
- deadbeef: The SHA-1 hash of an arbitrary commit
- bugfix42: A current development branch
- v1.0: A historical tag somewhere in history
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
Path limiting
Compare only part of the tree-ish objects:
$ git diff v4.19 v4.20 -- scripts
diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include
index ce53639a864a..3d09844405c9 100644
--- a/scripts/Kbuild.include
+++ b/scripts/Kbuild.include
@@ -138,17 +138,9 @@ cc-option-yn = $(call try-run,\
cc-disable-warning = $(call try-run,\
$(CC) -Werror $(KBUILD_CPPFLAGS) $(CC_OPTION_CFLAGS) -W$(strip $(1)) -c -x c /dev/null -o "$$TMP",-Wno-$(strip $(1)))
-# cc-name
-# Expands to either gcc or clang
-cc-name = $(shell $(CC) -v 2>&1 | grep -q "clang version" && echo clang || echo gcc)
-
# cc-version
cc-version = $(shell $(CONFIG_SHELL) $(srctree)/scripts/gcc-version.sh $(CC))
-# cc-fullversion
-cc-fullversion = $(shell $(CONFIG_SHELL) \
- $(srctree)/scripts/gcc-version.sh -p $(CC))
-
# cc-ifversion
# Usage: EXTRA_CFLAGS += $(call cc-ifversion, -lt, 0402, -O1)
cc-ifversion = $(shell [ $(cc-version) $(1) $(2) ] && echo $(3) || echo $(4))
... lots of output snipped ...
Popular options with git diff
Sometimes you don't need to see all of the output:
$ git diff v4.19 v4.20 -- scripts/dtc | wc -l 3375 $
--stat
$ 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(-) $
--name-only
$ 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 $
Whitespace issues
--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.