Skip to content
Latchkey

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 them
  • HEAD - 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →