Order of commit arguments in git diff

TL;DR

In the following git-diff syntax,

git diff [--options] <commit> <commit> [--] [<path>...]
  • the first <commit> corresponds to the base commit,
  • the second <commit> corresponds to the commit to compare to the base commit.

Using a mathematically inspired notation,

git diff <x> <x+∆x>

will show you the difference ∆x, whereas

git diff <x+∆x> <x>

will show you the difference -∆x.

Note that, because the two commits need not be ordered in any way, either chronologically or topologically, calling them “old” and “new” (as you do) is a bit misleading.

More details

You can learn a great deal simply by looking up the git-diff man page. Under the Description section, you’ll find

git diff [--options] <commit> <commit> [--] [<path>...]

This is to view the changes between two arbitrary <commit>.

Granted, that doesn’t tell you which commit is which, but, further down, under the Examples section, you’ll find a couple of illuminating examples:

git diff HEAD^ HEAD

[…]
Compare the version before the last commit and the last commit.

and

git diff topic...master

[…]

Changes that occurred on the master branch since when the topic branch was started off it.

Leave a Comment

tech