Here is an older cheat sheet I’ve found again with some git tricks to get data from source code repositories for data-driven software analytics or for getting some initial insights into a software development project.

Start to map author names to persons via .mailmap file

Command

git shortlog -sne | cut -f2 | sort > .mailmap

Example content of .mailmap

Alice Andersson <alice.andersson@example.com>
Bruno Beck <bruno.beck@example.org>
Carla Costa <carla.costa@example.net>
David Dubois <david.dubois@example.com>
Emma Eriksson <emma.eriksson@example.org>

Count the number of commits per file

Command

git log --no-merges --no-renames --numstat --pretty=format:"" -- *.java | cut -d$'\t' -f3 | grep -v '^$' | sort | uniq -c | sort

Example output

16 src/test/java/com/example/app/service/AbstractServiceTests.java
18 src/main/java/com/example/app/repository/jdbc/JdbcFooRepositoryImpl.java
18 src/main/java/com/example/app/web/FooController.java
19 src/main/java/com/example/app/web/BarController.java
23 src/main/java/com/example/app/repository/jdbc/JdbcBarRepositoryImpl.java

Search for commit activity per author

Command

git shortlog -ns -- **Repository**.java

Example output

45 Alice Andersson
12 Bruno Beck
6 Carla Costa
2 David Dubois
1 Emma Eriksson

Search for awkward things

e. g. “todos” and “fixmes”

git grep --perl-regexp "\/\/ *(todo|fixme)"

e. g. commented code

git grep --perl-regexp " \/\/.*(=|;)" -- *.java

Browse through the commit messages

Command

git log --pretty=format:"%h %s"

Example output

a1b2c3d removed appserver-specific files
e4f5g6h added a UI library
i7j8k9l used output escaping to prevent HTML injection
m0n1o2p migrated all dates to a modern date/time library
q3r4s5t reorganized view folders and updated navbar

Count last changed line in each Java source code file per author

Command

find . -type d -name ".git" -prune -o -type f \( -iname "*.java" \) | xargs -n1  git blame -w -f -C -M  --date=format:"|" | cut -d"|" -f 1 | cut -d"(" -f 2 | sed 's/\s*$//' | sort | uniq -c | sort

Example output

152 Alice Andersson
364 Bruno Beck
418 Carla Costa
1226 David Dubois
1718 Emma Eriksson
print
Software Archaeology with Git

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.