Searching and Statistics: git grep, shortlog, .mailmap and Repository Numbers
Search code with git grep, in the working tree, old revisions and every branch, with context, functions and combined patterns. Summarize contributors with git shortlog, merge identities with .mailmap, and count commits, lines and objects with rev-list, log, git repo and last-modified.
kvi5hnuCode Sprout team member6 min readOn this page
Two kinds of questions come up in every project: "where is this used?" and "who works on what, and how much?". Git answers the first with git grep, a fast search over tracked files and any revision. It answers the second with git shortlog and a handful of counting commands, made accurate by a .mailmap file. This article covers both.
git grep: search tracked files
git grep works like grep -r, but searches only tracked files, so node_modules, build output and other ignored files are skipped automatically, and it's very fast on large repositories:
git init -q trail-guide && cd trail-guide
cat > calc.js <<'EOF'
function total(values) {
return values.reduce((sum, v) => sum + v, 0);
}
function average(values) {
// TODO handle empty lists
return total(values) / values.length;
}
EOF
echo "TODO: write the docs" > README.md
git add .; git commit -q -m "Add calculator"
echo "=== -n: line numbers"
git grep -n TODO
echo "=== -c: count per file; -l: file names only"
git grep -c TODO
git grep -l total
echo "=== -p: show the enclosing function"
git grep -p "handle empty" -- '*.js'
echo "=== -W: the whole function"
git grep -W "handle empty"
=== -n: line numbers
README.md:1:TODO: write the docs
calc.js:6: // TODO handle empty lists
=== -c: count per file; -l: file names only
README.md:1
calc.js:1
calc.js
=== -p: show the enclosing function
calc.js=function average(values) {
calc.js: // TODO handle empty lists
=== -W: the whole function
calc.js=function average(values) {
calc.js: // TODO handle empty lists
calc.js- return total(values) / values.length;
calc.js-}
The common options:
| Option | Effect |
|---|---|
-n | line numbers. Set grep.lineNumber=true to always show them |
-i | ignore case |
-w | whole words only |
-c / -l / -L | count per file / names of files that match / that don't |
-E / -F / -P | extended regular expressions / fixed strings / Perl-compatible ones (if Git was built with them) |
-p | show the function each match is in |
-W | show the whole function around each match |
-A n / -B n / -C n | lines of context after / before / around matches |
--untracked | also search untracked files, and --no-exclude-standard adds ignored ones too |
--no-index | search a folder that isn't a Git repository |
--recurse-submodules | search submodules too |
Limit the search with a pathspec after --: git grep TODO -- '*.js' ':!vendor/'. The :! prefix excludes paths (article 5).
Combining patterns
git grep -e total --and -e return # lines containing both
git grep -e TODO --or -e FIXME # either (the default with several -e)
git grep -e total --and --not -e return # one but not the other
git grep --all-match -l -e TODO -e docs # files containing both, on any lines
--and, --or and --not work on single lines, and --all-match on whole files.
Searching other revisions
git grep can search any commit, tag or branch without checking it out:
git init -q trail-guide && cd trail-guide
echo 'function legacyTotal(v) { return v; }' > calc.js; git add .; git commit -q -m "Add calculator"
git tag v1.0
echo 'function total(v) { return v; }' > calc.js; git commit -q -am "Rename legacyTotal"
git switch -q -c experiment; echo 'legacyTotal(1)' > old-call.js; git add .; git commit -q -m "Old call"
git switch -q main
echo "=== working tree"
git grep -n legacyTotal; echo "(no matches, exit $?)"
echo "=== in v1.0 and in the experiment branch"
git grep -n legacyTotal v1.0 experiment
echo "=== in every commit of every branch (one line per commit and file)"
git grep -l legacyTotal $(git rev-list --all)
=== working tree
(no matches, exit 1)
=== in v1.0 and in the experiment branch
v1.0:calc.js:1:function legacyTotal(v) { return v; }
experiment:old-call.js:1:legacyTotal(1)
=== in every commit of every branch (one line per commit and file)
a3adf06b9e8d8de4f771cdbec0e75ad109eda0a5:old-call.js
e2688ae22cc45aea5cfc3ca39531768fb52d991a:calc.js
Results from revisions are prefixed with the revision: v1.0:calc.js:1:.... git grep <pattern> $(git rev-list --all) searches every commit, which is thorough but slow on big histories. To find when text was added or removed, git log -S is usually the better tool (article 30).
git shortlog: who contributed what
git shortlog groups commits by author. With -s it prints counts only, -n sorts by count, and -e shows emails:
git shortlog -sn # commits per author, most first
git shortlog -sne --no-merges # with emails, ignoring merge commits
git shortlog -sn --since=2026-01-01 main
git shortlog v1.0..v2.0 # who did what between two releases, with subjects
git shortlog -sn --group=committer # by committer instead of author
git shortlog -sn --group=author --group=trailer:co-authored-by # credit co-authors too
Without -s, shortlog lists each author's commit subjects, which makes quick release notes. In a pipeline or script, give it a revision explicitly (git shortlog -sn HEAD), because without one it reads commits from standard input.
.mailmap: one person, one identity
People commit under several names and emails over the years: a work and a personal address, a typo, a name change. Statistics then count them as different people. A .mailmap file at the top of the repository maps those identities to one:
git init -q trail-guide && cd trail-guide
git commit -q --allow-empty -m "Add calculator" --author="Ana Diaz <ana@example.com>"
git commit -q --allow-empty -m "Fix typo" --author="ana <ana.diaz@old-laptop.example.com>"
git commit -q --allow-empty -m "Add maps" --author="Ravi Kumar <ravi@example.com>"
echo "=== before"
git shortlog -sne HEAD
cat > .mailmap <<'EOF'
# Proper Name <proper@email> Commit Name <commit@email>
Ana Diaz <ana@example.com> ana <ana.diaz@old-laptop.example.com>
EOF
echo "=== with .mailmap"
git shortlog -sne HEAD
git check-mailmap "ana <ana.diaz@old-laptop.example.com>"
=== before
1 Ana Diaz <ana@example.com>
1 Ravi Kumar <ravi@example.com>
1 ana <ana.diaz@old-laptop.example.com>
=== with .mailmap
2 Ana Diaz <ana@example.com>
1 Ravi Kumar <ravi@example.com>
Ana Diaz <ana@example.com>
The file accepts several forms:
| Line | Maps |
|---|---|
Proper Name <commit@email> | any commit with that email gets the proper name |
<proper@email> <commit@email> | that email to the proper email |
Proper Name <proper@email> <commit@email> | that email to the proper name and email |
Proper Name <proper@email> Commit Name <commit@email> | that exact name and email pair |
.mailmap is used by shortlog, blame, and git log's default output, through the log.mailmap setting, which is on by default. In custom formats, %aN and %aE give the mapped name and email, while %an and %ae keep the recorded ones. It doesn't change commits, which keep their recorded identities. Commit the file so everyone's statistics agree. git check-mailmap tests a mapping. Hosting services also link commits to accounts by email, so add old addresses to your account to have commits attributed there.
Counting things
A collection of commands for common numbers:
git init -q trail-guide && cd trail-guide
for n in 1 2 3; do
seq 1 $((n * 10)) > "data$n.txt"; git add .
GIT_AUTHOR_DATE="2026-0$n-15T10:00:00Z" git commit -q -m "Add data $n" --author="Ana Diaz <ana@example.com>"
done
seq 1 5 > notes.txt; git add .
GIT_AUTHOR_DATE="2026-03-20T10:00:00Z" git commit -q -m "Add notes" --author="Ravi Kumar <ravi@example.com>"
echo "commits on main: $(git rev-list --count main)"
echo "tracked files: $(git ls-files | wc -l)"
echo "=== commits per month"
git log --format=%ad --date=format:%Y-%m | sort | uniq -c
echo "=== lines added per author"
git log --format='author %aN' --numstat | awk '/^author/ {a=substr($0, 8); next} NF==3 {add[a]+=$1} END {for (a in add) print add[a], a}' | sort -rn
commits on main: 4
tracked files: 4
=== commits per month
1 2026-01
1 2026-02
2 2026-03
=== lines added per author
60 Ana Diaz
5 Ravi Kumar
More useful one-liners:
git rev-list --count main..feature # commits on feature not on main
git log --author="Ana" --oneline | wc -l # commits by one author (a regex)
git log --shortstat --author="Ana" --since=1.month | tail -n 1
git for-each-ref --sort=-committerdate --format='%(committerdate:short) %(refname:short)' refs/heads
git count-objects -vH # object count and repository size on disk
The for-each-ref line lists branches with the date of their last commit, newest first, which is handy for spotting stale branches (article 23).
git repo and git last-modified
Recent versions add commands for repository facts:
git repo info --keys # list available keys (Git 2.54)
git repo info object.format references.format layout.bare
git repo structure # a table of reference and object counts and sizes
git last-modified # the last commit that changed each file
git repo info (Git 2.52) prints settings like the object format and whether the repository is bare, in a script-friendly key=value form. git repo structure summarizes how many branches, tags, commits, trees and blobs there are, their sizes, and the largest objects: a quick health check on a large repository. git last-modified (Git 2.52) shows, for each file, the most recent commit that changed it, which is what hosting services show next to each file in their file browsers.
Keep statistics in perspective: commit and line counts measure activity, not value. A one-line fix can be worth more than a thousand generated lines.
What's current
git repo infoandgit repo structure(Git 2.52, extended through 2.56) report repository characteristics.git last-modified(Git 2.52) finds the latest commit for each file efficiently.git shortlog --group=trailer:<key>(Git 2.29) creditsCo-authored-bytrailers, as used by GitHub for paired commits.git grepin partial clones fetches missing file contents in bulk since Git 2.55, instead of one at a time.
Common questions
What's the difference between git grep and grep -r?
git grep searches only tracked files, skipping ignored and untracked ones unless asked, can search any revision without checking it out, and is usually faster. grep -r searches everything on disk.
How do I search for text in all branches?
git grep <pattern> $(git for-each-ref --format='%(refname)' refs/heads) for branch tips, or $(git rev-list --all) for every commit. For "which commit added this?", use git log -S <text> --all.
Why does git shortlog show someone twice?
They committed with different names or emails. Add a .mailmap entry mapping the variants to one identity.
My git shortlog -sn hangs in a script. Why?
Without a revision and when standard input isn't a terminal, shortlog reads a log from standard input. Pass a revision, such as git shortlog -sn HEAD.
Summary
git grepsearches tracked files fast.-n,-i,-w,-c,-l,-pand-Wcover most needs, and--and,--notand--all-matchcombine patterns.- Give revisions to search old versions or branches:
git grep <pattern> v1.0. git shortlog -sncounts commits per author.--group=trailer:co-authored-bycredits co-authors..mailmapmerges identities forshortlog,blameandlog, without changing commits.git rev-list --count,git log --format=... | uniq -c,--numstat,git count-objects -vH,git repo structureandgit last-modifiedanswer most "how many" questions.
Practice questions
How do you find every TODO in JavaScript files, with line numbers, excluding the vendor/ folder?
Answer
git grep -n TODO -- '*.js' ':!vendor/'. The pathspec after -- limits the search to .js files, and :!vendor/ excludes that folder.
git shortlog -sn lists "Mei Chen" and "mei" separately. How do you fix the counts for everyone on the team?
Answer
Add a line to a committed .mailmap file mapping the variant to the canonical identity, for example Mei Chen <mei@example.com> mei <mei@laptop.example.com>, using the exact name and email shown by git shortlog -sne. Commit it, and everyone's shortlog, blame and log output uses the mapped identity.
Which command tells you how many commits feature has that main doesn't?
Answer
git rev-list --count main..feature. The range main..feature selects commits reachable from feature but not main (article 29), and --count prints how many.
Review quiz
Test yourself on Part 6 with the Git Part 6 review quiz.
Previous: Finding the Commit That Broke Something With git bisect
Next: Git Configuration: Levels, Conditional Includes, One-Off Settings and Useful Defaults

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.