Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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.

2 Answers

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)}'

Reference: https://git-scm.com/docs/git-log

0 votes
by (2.8k points)

This command show you all about all the changes made in the commit, i.e. total number of lines added or deleted:

git diff --stat <commit1> <commit2>

To shows total number of lines added or deleted we use:

git diff --shortstat <commit1> <commit2>

This will show, the total changes, 

e.g., "8 files changed, 32 insertions(+), 16 deletions(-)".

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...