I found this answer already: Number of commits on branch in git
but that assumes that the branch was created from master.
How can I count the number of commits along a branch without relying on that assumption?
In SVN this is trivial, but for some reason is really difficult to figure out in git.
解决方案
To count the commits for the branch you are on:
git rev-list --count HEAD
for a branch
git rev-list --count
If you want to count the commits on a branch that are made since you created the branch
git rev-list --count HEAD ^
This will count all commits ever made that are not on the branch-name as well.
Examples
git checkout master
git checkout -b test
git rev-list --count HEAD ^master
Result: 3
If your branch comes of a branch called develop:
git checkout develop
git checkout -b test
git rev-list --count HEAD ^develop
Result: 3
Ignoring Merges
If you merge another branch into the current branch without fast forward and you do the above, the merge is also counted. This is because for git a merge is a commit.
If you don't want to count these commits add --no-merges:
git rev-list --no-merges --count HEAD ^develop