User Tools

Site Tools


This is an old revision of the document!


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


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

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


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/ | 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/     |   4 +-
 scripts/dtc/version_gen.h            |   2 +-
 17 files changed, 1615 insertions(+), 231 deletions(-)


$ git diff v4.14 v4.15 --name-only -- scripts/dtc/

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

Using ".." and "..."


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

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

The initial branches to compare

$ git show fix_license
commit 65633ac0f606e70db50460502989a7a74dca8e20 (fix_license)
Author: Robert P. J. Day <>
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 or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.

</code> $ git show fix_readme commit 5f1992a4245c301ed5e556f4ef47153981a67820 (fix_readme) Author: Robert P. J. Day 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. </code>

Whitespace issues

Just scratching the surface:

    Ignore carrige-return at the end of line when doing a

    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 changes whose lines are all blank.
git_diff.1551436312.txt.gz · Last modified: 2019/03/01 10:31 by rpjday