It is easy to count the lines of code between two branches and know the net delta. Is there a way to measure the added/edited lines of code in a git merge, ignoring the removed lines?
-
1Lines of code is not a good estimation of work because they don't correlate with amount of time spent. I recently had a colleague who fixed a long-standing bug with about 4 lines of code that had taken several people, both of us included, probably double-digit hours to even try to reproduce. – bk2204 Jan 10 '23 at 22:30
-
3I’m not asking for opinions about what metric is best. I am asking if there is a way to determine net positive lines of code from a git merge. – Nick Jan 11 '23 at 02:31
-
Compare the merge commit to its first parent, look at the status line. There's options to ask for just the stats in various formats. – jthill Jan 11 '23 at 02:52
2 Answers
The simplest way is to use git diff
and grep for that +
marker at the beginning of added lines, but removing +++
since it's for file paths.
git diff <sha-before-merge> <sha-after-merge> |
grep -v '^+++' |
grep '^+' |
wc -l
Although using --numstat
might be even easier:
git diff --numstat <sha-before-merge> <sha-after-merge> |
awk '{ s += $1 } END { print s }'
the git diff
command gives you the number of added lines in each file in the first column, and the awk
one-liner tallies it.

- 10,635
- 14
- 30
- 40
I initially accepted the answer from @joanis using numstat and AWK to parse the first column and total up the additions. This answer was helpful and got me pointed in the right direction. After reading about that option in the manual I noticed that --shortstat
gets me what I need without needing the extra step of useing AWK to parse out and total up a column. I am viewing this data manually so I do not need to use AWK or SED to extract the number from the second column, but that would be trivial if I needed to do it.
E.g:
git diff --shortstat <sha-before-merge> <sha-after-merge>
28 files changed, 138 insertions(+), 209 deletions(-)
I am further filtering the diff to exclude some automatically generated coverage data using the pathspec feature discussed in this answer. For me this was resulting in double counting the actual changes.
git diff --shortstat <sha-before-merge> <sha-after-merge> -- . ':(exclude)*.gcov*'
15 files changed, 39 insertions(+), 110 deletions(-)

- 1,361
- 1
- 14
- 42