Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (29.3k points)

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 line counts.

1 Answer

0 votes
by (50.2k points)

For this you need to use --stat option to git diff or if you are doing it in a script then use --numstat option

Let’s see how we implement it with --stat option:

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

--stat produces the human-readable output you're used to seeing after merges

you were looking to do this on multiple commits at the same time - that's a task for git log. Since git log internally calls the diff machinery to print requested information, 

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

 you can use this command for multiple commits at the same time.

Here's a one-liner to get total changes instead of pre-commit changes from git log (change the commit selection options as desired):

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


Browse Categories