Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of commit arguments in git diff

Tags:

git

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.

like image 974
user1825441 Avatar asked Jul 16 '15 12:07

user1825441


People also ask

How do you diff commits in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch. Order does matter when you're comparing branches.

How do I see diff before commit?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged . first.

How does git diff work internally?

Diffing is a function that takes two input data sets and outputs the changes between them. git diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.

How check diff after commit?

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit: git diff COMMIT~ COMMIT will show you the difference between that COMMIT 's ancestor and the COMMIT . See the man pages for git diff for details about the command and gitrevisions about the ~ notation and its friends.


1 Answers

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.

like image 199
jub0bs Avatar answered Oct 09 '22 05:10

jub0bs