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