Git Project Stats in Terminal: How-To Guide
In this article
This guide shows you how to display key statistics about your Git repository directly in your terminal. You'll learn to see the number of commits, lines of code, active branches, contributors, and recent activity without leaving the command line. This is useful for quick project health checks, reporting, or simply understanding the scope of a codebase. You'll need Git installed and a terminal (Command Prompt, PowerShell, or any Unix shell).
What You'll Need
- Git installed (check with
git --version) - A terminal or command prompt
- A Git repository to inspect (or you can initialize one with
git init) - Basic familiarity with running commands
Step-by-Step
- Open your terminal and navigate to your Git repository's root directory using
cd /path/to/your/repo. - Check your current branch and status by running
git status. This confirms you're in the right place and shows how many files are modified or untracked. - Count total commits with
git rev-list --count HEAD. This prints the number of commits on the current branch. For all branches, usegit rev-list --count --all. - List all branches with
git branch -a. The output shows local branches (prefixed with*for the current one) and remote branches (prefixed withremotes/). - Show contributors and their commit counts by running
git shortlog -sn. This gives a numbered list of authors sorted by number of commits. Add--allto include commits from all branches. - View recent commit history with
git log --oneline -10. This shows the last 10 commits in a compact format (short hash and message). Adjust the number to see more or fewer. - Get a summary of changes using
git log --stat. This shows each commit with the files changed and the number of insertions (+) and deletions (-) per file. - Count lines of code in the working directory with
git ls-files | xargs wc -l. This lists all tracked files and counts lines in each, ending with a total. For a cleaner output, usegit ls-files '*.py' | xargs wc -lto filter by file extension (e.g., Python). - Check for uncommitted changes with
git diff --stat. This shows a summary of changes not yet staged or committed, including how many lines were added and removed. - See the last commit date with
git log -1 --format=%ci. This outputs the date and time of the most recent commit in ISO format, useful for checking project activity. - Combine multiple stats into one view by writing a simple shell alias or function. For example, add this to your
.bashrcor.zshrc:
function gitstats() { echo "Commits: $(git rev-list --count HEAD)"; echo "Branches: $(git branch | wc -l)"; echo "Contributors: $(git shortlog -sn | wc -l)"; echo "Last commit: $(git log -1 --format=%ci)"; }
Then rungitstatsin any repo to see all four stats at once.
Common Mistakes
- Running commands outside a repository: Git commands like
git logorgit branchfail with "fatal: not a git repository" if you're not in a project folder. Alwayscdinto the repo first. - Confusing branches and commits:
git branchshows branches, not commit counts. Usegit rev-list --countfor commit totals. - Counting lines incorrectly:
git ls-files | xargs wc -lonly counts tracked files. Untracked files (new files not yet added) are excluded, which may undercount the actual code. - Using
git logwithout options: Plaingit logopens a pager (likeless) and can be overwhelming. Use--onelineand a number limit to keep output manageable. - Forgetting to fetch remote data:
git branch -ashows remote branches only if you've rungit fetchrecently. Remote stats may be outdated otherwise.
Tips
- Use
--allfor full project stats: Add--alltogit rev-listandgit shortlogto include commits from every branch, not just the current one. - Filter by author: To see one person's stats, use
git shortlog -sn --author="Name"orgit log --author="Name" --oneline | wc -l. - Check commit frequency: Use
git log --format=%ad --date=short | sort | uniq -cto see how many commits happened on each date, revealing activity patterns. - Save your favorite commands: Create aliases in your shell config, like
alias gcount='git rev-list --count HEAD', to avoid retyping long commands. - Use
git status -sfor a compact status: The short format shows modified files with a single character prefix, making it easier to scan. - Combine with
git diff --numstat: This shows per-file added and deleted lines in a tabular format, ideal for scripts or quick analysis.
Troubleshooting
- "fatal: not a git repository": You're in the wrong directory. Run
pwdto see where you are, thencdto the repo root (where the.gitfolder is). - Command not found (e.g.,
xargson Windows): Windows Command Prompt lacks Unix tools. Use PowerShell instead, or install Git Bash (included with Git for Windows) which provides these commands. - Output is too long or frozen: Git commands like
git logmay open a pager. Pressqto exit, or add--no-pagerbefore the command (e.g.,git --no-pager log --oneline). - Line count shows "No such file or directory": This happens when a tracked file was deleted but not committed. Run
git statusto see deleted files and commit the removal withgit add -A && git commit -m "remove deleted files". - Contributors list is empty: If
git shortlogshows nothing, you likely have no commits on the current branch. Check withgit log --oneline; if empty, switch to a branch that has commits or make your first commit. - Stats don't match what you expect: Remember that Git only tracks committed changes. Uncommitted work won't appear in
git logorgit shortlog. Usegit diff --statfor uncommitted changes.
Frequently Asked Questions
Q: How do I count all commits across all branches?
A: Run git rev-list --count --all. This counts every commit reachable from any branch or tag, giving the total project history.
Q: Can I see which files have the most lines?
A: Yes, run git ls-files | xargs wc -l | sort -n. This lists files sorted by line count, with the largest at the end. Use tail -10 to see the top 10.
Q: How do I show stats for a specific time period?
A: Use date filters with git log. For example, git log --since="2023-01-01" --until="2023-12-31" --oneline | wc -l counts commits in that year. Combine with --author for per-person stats.
Q: What does git shortlog -sn actually show?
A: It shows a list of authors sorted by commit count, with the number of commits next to each name. The -s suppresses the commit summaries, and -n sorts numerically instead of alphabetically.
Q: Is there a way to see lines added vs. deleted total?
A: Yes, use git log --shortstat --oneline | grep -E "files? changed" | awk '{files+=$1; inserted+=$4; deleted+=$6} END {print "Files changed:", files, "Insertions:", inserted, "Deletions:", deleted}'. This sums the per-commit stats into totals.
Q: Why does my line count include binary files?
A: wc -l counts lines in any file, including binaries, which can give misleading numbers. Filter by extension, e.g., git ls-files '*.js' '*.ts' | xargs wc -l, or exclude known binary types with git ls-files | grep -v '\.png$'.