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.
views:
200answers:
3git 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
.
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)
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(-)