16

Is there a simple way I can ask git for the amount of lines I added (or add and removed) in a specific date range?

I'm using git on Windows, Linux and TortoiseGit(Windows)

Yue Lin Ho
  • 2,945
  • 26
  • 36
  • 1
    Something both posters seem to have missed is that there's a `--numstat` option, which gives a much more machine-readable format than `--stat`. – Cascabel Jun 05 '11 at 13:23
  • 1
    @Jefromi: What makes you think I missed it? ' 1 files changed, 2 insertions(+), 0 deletions(-)' is no harder to parse in `awk` than '2 0 .gitconfig' and saves me tallying the number of files changed! – johnsyweb Jun 06 '11 at 02:19
  • Also check out [@quorian's great answer](http://stackoverflow.com/a/9933440/24874) which gives counts of lines added, removed and modified. – Drew Noakes Nov 26 '13 at 00:33

3 Answers3

24

Building upon Seth Robertson's answer, (+1 Seth!) awk will tally up the columns for you:

% git log --stat --author $(git config --get user.email) --since="last year" --until="last month" | awk -F',' '/files? changed/ {
    files += $1
    insertions += $2
    deletions += $3
    print
}
END {
    print "Files Changed: " files
    print "Insertions: " insertions
    print "Deletions: " deletions
    print "Lines changed: " insertions + deletions

}'

 

Community
  • 1
  • 1
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • This seems useful, but how do I invoke this in Terminal? Not familiar with `awk`. – Wallace Sidhrée Feb 09 '13 at 12:13
  • @WallaceSidhrée: In [tag:Bash] or [tag:Zsh], you can just past my answer straight in (without the leading `% `. Or you could put this into a script somewhere in your `${PATH}`. – johnsyweb Feb 09 '13 at 20:01
  • This can yield incorrect results! The summary line of `stat` may hide `deletions` or `insertions` if there are none. If there are no insertions in that particular commit, then git may show `1 file changed, 6 deletions(-)`, making your script add the deletions to `insertions` variable. – thrau Mar 19 '16 at 21:55
  • one could modify the awk script to use matching `match($0, /([0-9]+) files? changed(, ([0-9]+) insertions?\(\+\))?(, ([0-9]+) deletions?\(\-\))?/, m); files += m[1]; insertions += m[3]; deletions += m[5];` – thrau Mar 20 '16 at 10:50
  • Thanks @thrau. The git output has changed a little in the five years since I wrote this post. – johnsyweb Mar 20 '16 at 11:08
  • 1
    your answer was definitely correct at the time! the comments in this answer http://stackoverflow.com/a/13056003/804840 indicate that the output changed during a 1.7.9 -> 1.7.10 patch. – thrau Mar 20 '16 at 11:39
  • @Johnsyweb are you sure about the no of lines changed obtain through `insertions-deletions`, as not only the lines that are caused by modification, the line which deleted are also in the count of deletions. so how do we identify them exactly – Kasun Siyambalapitiya Dec 07 '16 at 07:14
9
git log --stat --author me --since="last year" --until="last month"

You can then post-process the --stat information at the bottom.

Seth Robertson
  • 30,608
  • 7
  • 64
  • 57
2

In case someone is interested in an overall statistics of the repo:

  1. Right click on the repo folder, select TortoiseGit/Show Log.
  2. Click Statistics at the bottom of the dialog. Statistics
zwcloud
  • 4,546
  • 3
  • 40
  • 69