Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Git Project Stats in Terminal: How-To Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

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

  1. Open your terminal and navigate to your Git repository's root directory using cd /path/to/your/repo.
  2. 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.
  3. Count total commits with git rev-list --count HEAD. This prints the number of commits on the current branch. For all branches, use git rev-list --count --all.
  4. List all branches with git branch -a. The output shows local branches (prefixed with * for the current one) and remote branches (prefixed with remotes/).
  5. Show contributors and their commit counts by running git shortlog -sn. This gives a numbered list of authors sorted by number of commits. Add --all to include commits from all branches.
  6. 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.
  7. 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.
  8. 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, use git ls-files '*.py' | xargs wc -l to filter by file extension (e.g., Python).
  9. 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.
  10. 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.
  11. Combine multiple stats into one view by writing a simple shell alias or function. For example, add this to your .bashrc or .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 run gitstats in any repo to see all four stats at once.

Common Mistakes

  • Running commands outside a repository: Git commands like git log or git branch fail with "fatal: not a git repository" if you're not in a project folder. Always cd into the repo first.
  • Confusing branches and commits: git branch shows branches, not commit counts. Use git rev-list --count for commit totals.
  • Counting lines incorrectly: git ls-files | xargs wc -l only counts tracked files. Untracked files (new files not yet added) are excluded, which may undercount the actual code.
  • Using git log without options: Plain git log opens a pager (like less) and can be overwhelming. Use --oneline and a number limit to keep output manageable.
  • Forgetting to fetch remote data: git branch -a shows remote branches only if you've run git fetch recently. Remote stats may be outdated otherwise.

Tips

  • Use --all for full project stats: Add --all to git rev-list and git shortlog to 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" or git log --author="Name" --oneline | wc -l.
  • Check commit frequency: Use git log --format=%ad --date=short | sort | uniq -c to 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 -s for 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 pwd to see where you are, then cd to the repo root (where the .git folder is).
  • Command not found (e.g., xargs on 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 log may open a pager. Press q to exit, or add --no-pager before 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 status to see deleted files and commit the removal with git add -A && git commit -m "remove deleted files".
  • Contributors list is empty: If git shortlog shows nothing, you likely have no commits on the current branch. Check with git 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 log or git shortlog. Use git diff --stat for 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$'.