git rev-list --count: Usage, Options & Common CI Errors
git rev-list --count prints how many commits a range contains.
Need to know how many commits a branch is ahead, or how many landed since a tag? rev-list --count gives a single number that is easy to compare in CI conditions.
What it does
git rev-list --count walks the requested revision range and prints only the count of matching commits, instead of listing their SHAs.
Common usage
Terminal
git rev-list --count HEAD
git rev-list --count main..feature # commits ahead of main
git rev-list --count --left-right main...feature
git rev-list --count v1.0.0..HEAD # commits since a tagOptions
| Flag | What it does |
|---|---|
| --count | Print the number, not the list |
| <a>..<b> | Commits in b not in a |
| <a>...<b> | Symmetric difference |
| --left-right | With ..., split counts per side |
| --no-merges | Exclude merge commits |
Common errors in CI
A two-dot range (main..feature) needs both refs present; on a shallow clone the merge base may be missing and the count is wrong or errors. Use fetch-depth: 0. Note .. (asymmetric) and ... (symmetric) differ - mixing them up gives surprising ahead/behind numbers.
Related guides
git rev-list: Usage, Options & Common CI Errorsgit rev-list lists commit objects in reverse order, the plumbing behind counting and walking history. Referen…
git merge-base: Usage, Options & Common CI Errorsgit merge-base finds the common ancestor of two commits, the basis for diffing a PR. Reference for --fork-poi…
git cherry: Usage, Options & Common CI Errorsgit cherry shows which commits on a branch have or have not been applied upstream. Reference for -v, the + /…
git log: Usage, Options & Common CI Errorsgit log shows commit history with flexible formatting. Reference for --oneline, --graph, --pretty, ranges, an…