35

Is there a way in git to count the total deletions and additions for a given user on a given branch? Something like that is on github, in the graph section there is a chart that shows you the total additions and deletions but only on the master branch... i think if they did it this mus be possible in git also, so, does someone know how to do that?

Thank you in advance.

Starlays
  • 1,039
  • 2
  • 15
  • 29

4 Answers4

41

I don't think Git has any built-in command that does this. But with the help of some other standard utilities, it can be done. Here is an example that filters Git's log output through awk to get the summary of total insertions and deletions:

git log --author=$USER --shortstat $BRANCH | \
awk '/^ [0-9]/ { f += $1; i += $4; d += $6 } \
END { printf("%d files changed, %d insertions(+), %d deletions(-)", f, i, d) }'
Dan Moulding
  • 211,373
  • 23
  • 97
  • 98
  • I don't understand why the values reported by git are different from the ones reported on github. – Starlays Oct 24 '12 at 20:01
  • @Starlays: I sometimes think of `committer` and `author` as being synonymous, but they're not. I changed the answer to use `author` instead. Also, on one of my GitHub projects I've made commits from different accounts (different user.name and user.email). GitHub's graphs for that project are only showing the additions and deletions from one of those accounts, even though all of the email addresses are linked to my GitHub account. Seems like a bug. If I specify `$USER` in the above command such that it will only match the one account that GitHub is showing in the graph, then the values match. – Dan Moulding Oct 25 '12 at 11:38
  • 6
    **This works great as long as there are insertions**. In the case where there would be only deletions, they would count as insertions, because the 4th word on `8 files changed, 81 deletions` is deletion, not insertion. That would explain the difference between the output on Github, which @Starlays mentioned. – Wallace Sidhrée Feb 10 '13 at 21:35
  • @WallaceSidhrée: In Git 1.7.9, against which this was tested, in the absence of insertions, it writes something like this `11 files changed, 0 insertions(+), 327 deletions(-)`. The fourth field is a zero. Is your version of Git older or newer? – Dan Moulding Feb 11 '13 at 13:37
  • 1
    @DanMoulding, thanks for the heads-up, didn't even consider git version... My git version is 1.7.10.2, though, and I do get a line like `1 file changed, 28 deletions(-)` after running `git log --author=myname --shortstat`. Both lines were copied/pasted from Terminal. – Wallace Sidhrée Feb 11 '13 at 14:06
7

As answered here: Git: How to get total number of +/- ( Insertions and Deletions) from a single git commit

Git offers the short statistics (total files changed, number of insertions, no. of deletions):

git diff --shortstat COMMIT_HASH~ COMMIT_HASH
devplayer
  • 587
  • 4
  • 12
5

I rolled my own based on this to cover the case with newer git where 0 insertions or deletions is not printed:

git log --author=$USER --shortstat $BRANCH | 
  ruby -e 'puts $<.read.scan(/(\d+) \w+\(([+-])\)/).group_by(&:last).
  map{|s,ds|s+ds.map(&:first).map(&:to_i).inject(0,&:+).to_s}.join(", ")'

This just prints the insertion and deletion totals, like: +7108, -6742

If you want the files changed total too, this slightly hacky¹ version will do:

git log --author=$USER --stat=99999999 $BRANCH | 
  ruby -e 'f,a,d = $<.read.scan(/^ (.*?)\s+\|\s+\d+\s(\+*)(\-*)$/).transpose;
  puts [f.uniq.length, " files, +", a.join.length, ", -", d.join.length].join'

The output looks like this: 97 files, +3701, -3598

The files total is the number of unique file names across all the commits, not the sum of the number of files changed on each commit, which is what the original answer gives you.


¹ The 999… thing is because we're literally counting the number of +s and -s, and we need git not to cap them to the default width, so we give it a very long width to work with.

kch
  • 77,385
  • 46
  • 136
  • 148
0

This is what I use and works right now on git version 2.37.1 (Apple Git-137.1):

git log --author="< INSERT YOUR NAME HERE>" --pretty=tformat: --numstat | grep -v '^-' | awk '{ add+=$1; rem$
} END { print add,
remove }'

The accepted answer does not work for me (maybe due to changes to git, idk)

gustavz
  • 2,964
  • 3
  • 25
  • 47