Skip to content
LatchkeyLatchkey home

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.

Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached

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.

Frequently asked questions

git rev-list Command: Count Commits in CI?
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.
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.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card