Search code examples
gitgit-diff

Order of commit arguments in git diff


In what order does the git command

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

Compare the different commits against each other? It seems like if I want to compare the new against the old one I need to do

git diff [--options] <New_commit> <Old_commit> in order to see the current diff?

I usually do git diff [--options] <Old_commit> <New_commit>

But that seems to be wrong?

When I do for example

$ git diff `git rev-list --since="jun 30 2014" --reverse origin/master | head -1` `git rev-list --until="dec 31 2014" origin/master | head -1` --shortstat 

1072 files changed, 389650 insertions(+), 39180 deletions(-)

But when I do

$ git diff --stat `git rev-list --until="dec 31 2014" origin/master | head -1`

I get the printout that:

384 files changed, 61255 insertions(+), 20526 deletions(-)

Which is not near 300000. So my question is if I should insert the new commit first and the old commit sedond, like:

 $ git diff `git rev-list --until="dec 31 2014" origin/master | head -1`..`git rev-list --since="jun 30 2014" --reverse origin/master | head -1` 

I can't find any documentation about in which order I should insert the commits in order to see the difference between my new and my old commit. Perhaps you can clarify this for me?

Thanks in advance.

Edit: The reason I'm asking is that I want to

  1. know how many new lines of code that has been added to a new commit given an old commit, and

  2. I want to calculate the number of lines of code in the new commit.


Solution

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