tags:

views:

200

answers:

3

Is there any easy way to calculate the number of lines changed between two commits in git? I know I can do a git diff, and count the lines, but this seems tedious. I'd also like to know how I can do this, including only my own commits in the linecounts.

+1  A: 
git diff --stat commit1 commit2

EDIT: You have to specify the commits as well (without parameters it compares the working directory against the index). E.g.

git diff --stat HEAD^ HEAD

to compare the parent of HEAD with HEAD.

Matthew Flaschen
There's never really any need to use `diff-index` - the `diff` frontend can handle everything; the case of `diff-index` is covered by the `--cached/--staged`, I believe. (And there's no way to use `diff-index` to compare two arbitrary commits as the OP asked.)
Jefromi
The output of this is nothing for me.
Mike
@Mike: Did you leave off a carat? Was your most recent commit a merge commit? If git says there's no diff, it's because there's no diff.
Jefromi
or `git diff --stat HEAD^!`
Jakub Narębski
+6  A: 

You want the --stat option of git diff, or if you're looking to parse this in a script, the --numstat option.

git diff --stat <commit-ish> <commit-ish>

--stat produces the human-readable output you're used to seeing after merges; --numstat produces a nice table layout that scripts can easily interpret.

I somehow missed that you were looking for doing this on multiple commits at the same time - that's a task for git log. Ron DeVera touches on this, but you can actually do a lot more than what he mentions. Since git log internally calls the diff machinery in order to print requested information, you can give it any of the diff stat options - not just --shortstat. The one single most likely answer you want is:

git log --author="Your name" --stat <commit1>..<commit2>

but you can use --numstat or --shortstat as well. git log can also select commits in a variety other ways - have a look at the documentation. You might be interested in things like --since (rather than specifying commit ranges, just select commits since last week) and --no-merges (merge commits don't actually introduce changes), as well as the pretty output options (--pretty=oneline, short, medium, full...).

Here's a one-liner to get total changes instead of per-commit changes from git log (change the commit selection options as desired - this is commits by you, from commit1 to commit2):

git log --numstat --pretty="%H" --author="Your Name" commit1..commit2 | awk 'NF==3 {plus+=$1; minus+=$2} END {printf("+%d, -%d\n", plus, minus)}'

(you have to let git log print some identifying information about the commit; I arbitrarily chose the hash, then used awk to only pick out the lines with three fields, which are the ones with the stat information)

Jefromi
Is there any way to include just my own commits?
Mike
@Mike: edited in!
Jefromi
+1  A: 

Assuming that you want to compare all of your commits between abcd123 and wxyz789, inclusive:

git log abcd123^..wxyz789 --oneline --shortstat --author="Mike Surname"

This gives succinct output like:

abcd123 Made things better
 3 files changed, 14 insertions(+), 159 deletions(-)
wxyz789 Made things more betterer
 26 files changed, 53 insertions(+), 58 deletions(-)
Ron DeVera
The output of this is nothing for me (I've made commits and verified --author is correct by using it with git log and no other arguments).
Mike