Counting Commits per Author Across All Branches in Git: An In-Depth Analysis of git shortlog Command

Dec 06, 2025 · Programming · 11 views · 7.8

Keywords: Git | commit statistics | branch management

Abstract: This article provides a comprehensive exploration of how to accurately count commits per author across all branches in the Git version control system. By analyzing the core parameters of the git shortlog command, particularly the --all and --no-merges options, it addresses issues of duplicate counting and merge commit interference in cross-branch statistics. The paper explains the command's working principles in detail, offers practical examples, and discusses extended applications, enabling readers to master this essential technique.

Introduction

In software development, the Git version control system has become an indispensable tool. For team collaboration, understanding each developer's contribution is a crucial aspect of project management. A common requirement is to count the number of commits per author across all branches, but this is not a straightforward task. Initial attempts using the git shortlog -s -n command reveal that it only counts commits on the current branch, ignoring unmerged commits from other branches. Iterating this command over every branch leads to duplicate counting of common commits. This paper delves into how to accurately achieve this goal using Git commands.

Core Solution: Detailed Explanation of git shortlog Command

Git provides the git shortlog command to summarize commit history. By combining specific parameters, one can efficiently solve cross-branch counting issues. The key command is:

git shortlog --summary --numbered --all --no-merges

Let's analyze each parameter's role:

After executing this command, the output typically appears as:

   120 John Doe
    80 Jane Smith
    50 Alex Johnson

Where the number indicates the commit count, followed by the author's name. This concise format facilitates quick analysis of team contribution distribution.

Technical Principles and Implementation Mechanism

Git's commit history is essentially a directed acyclic graph (DAG), where each commit node includes author, timestamp, and parent commit pointers. When using the --all parameter, git shortlog traverses all branch references (i.e., all pointers to commits), thereby covering the entire commit graph. Algorithmically, it collects all relevant commits by recursively traversing parent commits, then groups and counts them by author.

Importantly, Git automatically handles duplicate commit counting. Since each commit has a unique hash value, when the same commit is traversed from different branch references, it is counted only once. This avoids the duplicate counting errors that can occur with manual branch iteration.

The --no-merges parameter works by checking the number of parent nodes in a commit: merge commits typically have two or more parents, while regular commits have only one. Filtering out these merge commits makes the statistics more reflective of developers' code modification activities.

Practical Applications and Examples

Suppose a project has multiple feature branches and long-term maintenance branches. To obtain global commit statistics, simply run in the project root directory:

git shortlog -s -n --all --no-merges

For large codebases, add --since and --until parameters to limit the time range, e.g., counting commits in 2023:

git shortlog -s -n --all --no-merges --since="2023-01-01" --until="2023-12-31"

You can also combine --grep to filter specific commit messages or use --author to specify particular authors. These extended features make git shortlog a powerful code analysis tool.

Comparison with Other Methods

Beyond the above command, developers sometimes try other approaches, such as using git log with script processing. For example:

git log --all --no-merges --format="%an" | sort | uniq -c | sort -rn

This method can achieve similar results, but git shortlog as a built-in command is generally more efficient and easier to use. It is directly integrated into Git, requiring no additional scripts and reducing the likelihood of errors.

Considerations and Best Practices

When using git shortlog for statistics, keep the following points in mind:

  1. Ensure a relatively recent Git version to support all parameters. Older versions may lack options like --no-merges.
  2. Consistency in author names is crucial. If the same developer uses different names or emails across commits, statistics will be fragmented. It is recommended that teams standardize user information in Git configuration.
  3. For large projects with extensive history, command execution may take significant time. Consider adding --max-count limits or performing time-segmented statistics.
  4. Interpret results in context. For instance, high commit frequency may indicate active development or a rapid iteration development style, requiring evaluation alongside code quality.

Conclusion

Through the git shortlog --summary --numbered --all --no-merges command, we can efficiently and accurately count commits per author across all branches in a Git repository. This technique not only simplifies contribution analysis in project management but also demonstrates the flexibility and power of Git commands. Mastering this method helps teams better understand development dynamics and optimize collaboration processes. As the Git ecosystem continues to evolve, similar tools will remain vital supports for software development.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.