brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git rev-list with count gives you a commit count for scripts

Git
~/tips/tip-git-rev-list-count-commits

git rev-list --count HEAD

Prints a single number of commits, useful for build numbers and quick checks.

Counting commits with git log --oneline | wc -l works, but git has a command that prints only the number.

git rev-list --count HEAD

git rev-list is the plumbing command underneath git log: it lists commit hashes. With --count it prints how many it found and nothing else. It takes the same ranges as git log, so you can count just the commits on your branch:

git rev-list --count main..HEAD

I like that as a sanity check before a squash: if it says 47, something probably wasn’t cleaned up. Some people use the count from HEAD as a simple build number, like 1.4.$(git rev-list --count HEAD), because it grows with every commit on that branch.

Filters work too. --no-merges leaves merge commits out, and --author and --since narrow it down the way they do for git log.

Don’t trust the number in a shallow clone, which many CI systems make by default. A clone with --depth 1 only has one commit, so the count is 1. And as a build number it’s only unique per branch: two branches can have the same count with completely different commits, and a rewritten history can make it go down.

Further reading: git-rev-list documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link