User Tools

Site Tools


git_diff

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/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
$

--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
$

Comparing two arbitrary files

General form:

$ git diff <commit1>:<file2> <commit2>:<file2>

For example, after a rename:

$ git diff v4.18:Makefile v4.19:makefile

Whitespace issues

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.
git_diff.1550920080.txt.gz · Last modified: 2019/02/23 11:08 by rpjday