git rev-list Command: Count Commits in CI
git rev-list enumerates commits reachable from given refs, with powerful filtering.
rev-list is the plumbing behind log. In CI its most common use is --count to turn the number of commits into a build number or to measure how far ahead a branch is.
Common flags
--count- print the number of matching commits instead of listing themHEAD- count commits reachable from HEAD<from>..<to>- count commits in a range (e.g. commits since a tag)--first-parent- follow only the first parent of merges--no-merges- exclude merge commits from the count--all- consider all refs
Example
shell
# Derive a monotonic build number from commit count
BUILD_NUMBER="$(git rev-list --count HEAD)"
# How many commits since the last tag?
git rev-list --count "$(git describe --tags --abbrev=0)..HEAD"In CI
git rev-list --count HEAD gives an ever-increasing build number that needs full history, so do not run it on a shallow clone. Use a tag..HEAD range to count commits in a release for changelog stats.
Key takeaways
- git rev-list --count HEAD yields a monotonic build number from commit history.
- It requires full history, so deepen shallow CI clones before counting.
- A tag..HEAD range counts exactly the commits in a release.
Related guides
git log Command: Inspect History in CIgit log shows commit history. Reference for --oneline, -n, and --format to extract commit messages and author…
git describe Command: Version Strings in CIgit describe builds a human-readable version from tags. Reference for --tags and --always to derive build ver…
git rev-parse Command: Resolve Refs in CIgit rev-parse resolves refs to SHAs and prints repo metadata. Reference for HEAD, --short, and --abbrev-ref t…
git shortlog Command: Summarize Authors in CIgit shortlog groups commits by author. Reference for -s, -n, and ranges to build contributor summaries and re…