102

Previously I have enjoyed TortoiseSvn's ability to generate simple commit stats for a given SVN repository. I wonder what is available in Git and am particularly interested in :

  • Number of commits per user
  • Number of lines changed per user
  • activity over time (for instance aggregated weekly changes)

Any ideas?

Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155

12 Answers12

212

Actually, git already has a command for this:

git shortlog

in your case, it sounds like you're interested in this form:

git shortlog -sne

See the --help for various options.

You may also be interested in the GitStats project. They have a few examples, including the stats for the Git project. From the GitStat main page:

Here is a list of some statistics generated currently:

  • General statistics: total files, lines, commits, authors.
  • Activity: commits by hour of day, day of week, hour of week, month of year, year and month, and year.
  • Authors: list of authors (name, commits (%), first commit date, last commit date, age), author of month, author of year.
  • Files: file count by date, extensions
  • Lines: Lines of Code by date
Pat Notz
  • 208,672
  • 30
  • 90
  • 92
  • 1
    This is exactly what I was looking for. Amazing that you can actually replace the code lines in my example with "git shortlog -sn" Vote up for this answer – Jesper Rønn-Jensen Sep 28 '09 at 18:16
  • 16
    also `git shortlog -sn --no-merges` remove "merge commits" from the count. – lbolla Dec 09 '11 at 09:52
  • 5
    february 2010: [Linus attacks!](http://gitstats.sourceforge.net/examples/git/authors.html#cumulated_added_lines_of_code_per_author) – naught101 Jun 15 '12 at 01:59
  • 1
    **Number of lines of code by author** using basic git commands (no need to install gitstats): `git ls-files | while read f; do git blame -w -M -C -C --line-porcelain "$f" | grep -I '^author '; done | sort -f | uniq -ic | sort -n --reverse` – hartmut Dec 03 '20 at 12:16
  • `git shortlog -sne | sort -k2 -rn | nl` for ranking the enteries in the output. If output is too big then less can be appended to make it scrolllable `git shortlog -sne | sort -k2 -rn | nl| less` – Raheel Sep 20 '21 at 15:01
26

First, you don't have to pull anything (as in network pull), because you have the whole repository and the whole history locally. I'm pretty sure there are tools that will give you statistics, but sometimes you can just be creative with the command lines. For instance, this (just out of my head) will give you the number of commits per user:

git log --pretty=format:%ae \
| gawk -- '{ ++c[$0]; } END { for(cc in c) printf "%5d %s\n",c[cc],cc; }'

Other statistics you asked for may need more thought put into it. You may want to see the tools available. Googling for git statistics points to the GitStats tool, which I have no experience with and even less idea of what it takes to get it run on windows, but you can try.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • 6
    `git shortlog -s -n` (from http://stackoverflow.com/questions/1828874/generating-statistics-from-git-repository) – naught101 Jun 15 '12 at 01:56
  • 4
    `git shortlog` is indeed the right command here but even without it, the complex awk command above can be repaced by `| sort | uniq -c` – josch Jan 06 '15 at 11:35
10

The best tool so far I identfied is gitinspector. It give the set report per user, per week etc

You can install like below with npm

npm install -g gitinspector

Details to get the links are below

https://www.npmjs.com/package/gitinspector
https://github.com/ejwa/gitinspector/wiki/Documentation
https://github.com/ejwa/gitinspector

example commands are

gitinspector -lmrTw
gitinspector --since=1-1-2017

etc

lrnzcig
  • 3,868
  • 4
  • 36
  • 50
Ravikiran Reddy Kotapati
  • 2,485
  • 3
  • 22
  • 27
7

Thanks to hacker for answering this question. However, I found these modified versions to be better for my particular usage:

git log --pretty=format:%an \
| awk '{ ++c[$0]; } END { for(cc in c) printf "%5d %s\n",c[cc],cc; }'\
| sort -r

(using awk as I don't have gawk on my mac, and sorting with most active comitter on top.) It outputs a list like so:

 1205 therikss
 1026 lsteinth
  771 kmoes
  720 minielse
  507 pagerbak
  269 anjohans
  205 mfoldbje
  188 nstrandb
  133 pmoller
   58 jronn
   10 madjense
    3 nlindhol
    2 shartvig
    2 THERIKSS
Jesper Rønn-Jensen
  • 106,591
  • 44
  • 118
  • 155
2

Here are ways to get stats for a specific branch or two hashs.

key here is the ability to do HASH..HASH

Below I am using the first hash from a branch to the HEAD which is the end of that branch.

Show total commits in a branch

  • git log FIRST_HASH..HEAD --pretty=oneline | wc -l
  • Output 53

Show total commits per author

  • git shortlog FIRST_HASH..HEAD -sne
  • Output
  • 24 Author Name
  • 9 Author Name
jhanifen
  • 4,441
  • 7
  • 43
  • 67
2

Note that, if your repo is on GitHub, you now (May 2013) have a new set of GitHub API to get interesting statistics.
See "File CRUD and repository statistics now available in the API"

That would include:

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

I've written a small shell script that calculates merge statistics (useful when dealing with a feature-branch-based workflow). Here's an example output on a small repository:

[$]> git merge-stats
% of Total Merges               Author  # of Merges  % of Commits
            57.14     Daniel Beardsley            4          5.63
            42.85        James Pearson            3         30.00
Xiong Chiamiov
  • 13,076
  • 9
  • 63
  • 101
  • Also, if you're using GitHub's pull request system, I wrote [github-pr-stats](https://github.com/xiongchiamiov/github-pr-stats), which provides much more data than git-merge-stats and even has a plugin system to allow you to integrate in your own company-specific analyses. – Xiong Chiamiov Jul 13 '16 at 01:16
1

Here is a simple ruby script that I used to get author, lines added, lines removed, and commit count from git. It does not cover commits over time.

Note that I have a trick where it ignores any commit that adds/removes more than 10,000 lines because I assume that this is a code import of some sort, feel free to modify the logic for your needs. You can put the below into a file called gitstats-simple.rb and then run

git log --numstat --pretty='%an' | ruby gitstats-simple.rb

contents of gitstats-simple.rb

#!/usr/bin/ruby

# takes the output of this on stdin: git log --numstat --prety='%an'

map = Hash.new{|h,k| h[k] = [0,0,0]}
who = nil
memo = nil
STDIN.read.split("\n").each do |line|
  parts = line.split
  next if parts.size == 0
  if parts[0].match(/[a-z]+/)
    if who && memo[0] + memo[1] < 2000
      map[who][0] += memo[0]
      map[who][1] += memo[1]
      map[who][2] += 1
    end
    who = parts[0]
    memo = [0,0]
    next
  end
  if who
    memo[0]+=line[0].to_i
    memo[1]+=parts[1].to_i
  end
end

puts map.to_a.map{|x| [x[0], x[1][0], x[1][1], x[1][2]]}.sort_by{|x| -x[1] - x[2]}.map{|x|x.inspect.gsub("[", "").gsub("]","")}.join("\n")
Cliff Frey
  • 11
  • 1
1

See this gitstat project

http://mirror.celinuxforum.org/gitstat/

0

DataHero now makes it easy to pull in Github data and get stats. We use it internally to track our progress on each milestone.

https://datahero.com/partners/github/

How we use it internally: https://datahero.com/blog/2013/08/13/managing-github-projects-with-datahero/

Disclosure: I work for DataHero

ibash
  • 1,477
  • 17
  • 31
0

You can use gitlogged gem (https://github.com/dexcodeinc/gitlogged) to get activities by author and date. This will give you report like this:

gitlogged 2016-04-25 2016-04-26

which returns the following output

################################################################

Date: 2016-04-25

Yunan (4):
      fix attachment form for IE (#4407)
      fix (#4406)
      fix merge & indentation attachment form
      fix (#4394) unexpected after edit wo

gilang (1):
      #4404 fix orders cart


################################################################
################################################################

Date: 2016-04-26

Armin Primadi (2):
      Fix document approval logs controller
      Adding git tool to generate summary on what each devs are doing on a given day for reporting purpose

Budi (1):
      remove validation user for Invoice Processing feature

Yunan (3):
      fix attachment in edit mode (#4405) && (#4430)
      fix label attachment on IE (#4407)
      fix void method (#4427)

gilang (2):
      Fix show products list in discussion summary
      #4437 define CApproved_NR status id in order


################################################################
Armin Primadi
  • 754
  • 6
  • 10
0

Modify https://stackoverflow.com/a/18797915/3243930 . the output is much closed to the graph data of github.

#!/usr/bin/ruby

# takes the output of this on stdin: git log --numstat --prety='%an'

map = Hash.new{|h,k| h[k] = [0,0,0]}
who = nil
memo = nil
STDIN.read.split("\n").each do |line|
  parts = line.split("\t")
  next if parts.size == 0
  if parts[0].match(/[a-zA-Z]+|[^\u0000-\u007F]+/)
    if who
      map[who][0] += memo[0]
      map[who][1] += memo[1]
      if memo[0] > 0 || memo[1] > 0 
        map[who][2] += 1
      end
    end
    who = parts[0]
    memo = [0,0]
    next
  end
  if who
    memo[0]+=parts[0].to_i
    memo[1]+=parts[1].to_i
  end
end

puts map.to_a.map{|x| [x[0], x[1][0], x[1][1], x[1][2]]}.sort_by{|x| -x[1] - x[2]}.map{|x|x.inspect.gsub("[", "").gsub("]","")}.join("\n")
Community
  • 1
  • 1
Jimmy Shen
  • 240
  • 1
  • 12