Count the number of commits on a Git branch

I already found this answer: The number of commits in a branch in git, but this assumes that the branch was created from the wizard.

How can I count the number of commits along a branch without relying on this assumption?

In SVN, this is trivial, but for some reason it is really hard to understand in git.

+149
git
Jul 25 '12 at 19:37
source share
8 answers

To count the commits for the branch you are on:

git rev-list --count HEAD 

for the branch

 git rev-list --count <branch-name> 

If you want to count commits on a branch created since the branch was created

 git rev-list --count HEAD ^<branch-name> 

This will reckon with all commits ever made that are also not related to the branch name.

Examples

 git checkout master git checkout -b test <We do 3 commits> git rev-list --count HEAD ^master 

Result: 3

If your branch comes from a branch called develop :

 git checkout develop git checkout -b test <We do 3 commits> git rev-list --count HEAD ^develop 

Result: 3

Ignore merge

If you merge another branch into the current branch without fast-forwarding, and you do it higher, merging is also taken into account. This is because for git, merging is a commit.

If you do not want to read these commits, add --no-merges :

 git rev-list --no-merges --count HEAD ^develop 
+281
Jul 25 '12 at 19:58
source share

this line

 git shortlog -s -n 

will generate output like this

 135 Tom Preston-Werner 15 Jack Danger Canty 10 Chris Van Pelt 7 Mark Reid 6 remi 
+53
Mar 05 '13 at 13:53 on
source share

This may require a relatively new version of Git, but this works well for me:

 git rev-list --count develop..HEAD 

This gives me the exact number of commits in the current branch, which has a base on the main server.

The command in response to Peter, git rev-list --count HEAD ^develop includes many more commits, 678 versus 97 in my current project.

My commit history is linear in this thread, so YMMV, but it gives me the exact answer I wanted, namely: "How many commits have I added so far in this thread of this function?".

+33
Mar 29 '14 at 5:36
source share

How about git log --pretty=oneline | wc -l git log --pretty=oneline | wc -l

This should count all commits from the point of view of your current branch.

+4
Jul 25 '12 at 19:40
source share

I like to do git shortlog -s -n --all . Gives you a list of names and the number of commits in a leaderboard style.

+3
Sep 20 '17 at 16:44
source share

One way to do this is to list the log for your branch and count the lines.

 git log <branch_name> --oneline | wc -l 
+1
Jul 25 '12 at 19:43
source share

Well, the selected answer does not work if you have separated your branch from a non-specific branch (i.e. not master or develop ).

Here I propose another way that I use in my pre-push git hooks.

 # Run production build before push echo "[INFO] run .git/hooks/pre-push" echo "[INFO] Check if only one commit" # file .git/hooks/pre-push currentBranch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') gitLog=$(git log --graph --abbrev-commit --decorate --first-parent HEAD) commitCountOfCurrentBranch=0 startCountCommit="" baseBranch="" while read -r line; do # if git log line started with something like "* commit aaface7 (origin/BRANCH_NAME)" or "commit ae4f131 (HEAD -> BRANCH_NAME)" # that means it on our branch BRANCH_NAME matchedCommitSubstring="$( [[ $line =~ \*[[:space:]]commit[[:space:]].*\((.*)\) ]] && echo ${BASH_REMATCH[1]} )" if [[ ! -z ${matchedCommitSubstring} ]];then if [[ $line =~ $currentBranch ]];then startCountCommit="true" else startCountCommit="" if [[ -z ${baseBranch} ]];then baseBranch=$( [[ ${matchedCommitSubstring} =~ (.*)\, ]] && echo ${BASH_REMATCH[1]} || echo ${matchedCommitSubstring} ) fi fi fi if [[ ! -z ${startCountCommit} && $line =~ ^\*[[:space:]]commit[[:space:]] ]];then ((commitCountOfCurrentBranch++)) fi done <<< "$gitLog" if [[ -z ${baseBranch} ]];then baseBranch="origin/master" else baseBranch=$( [[ ${baseBranch} =~ ^(.*)\, ]] && echo ${BASH_REMATCH[1]} || echo ${baseBranch} ) fi echo "[INFO] Current commit count of the branch ${currentBranch}: ${commitCountOfCurrentBranch}" if [[ ${commitCountOfCurrentBranch} -gt 1 ]];then echo "[ERROR] Only a commit per branch is allowed. Try run 'git rebase -i ${baseBranch}'" exit 1 fi 

For a more detailed analysis, visit my blog.

+1
Nov 06 '17 at 9:35 on
source share

You can also do git log | grep commit | wc -l

and get the result back

-2
mar 19 '17 at 6:56
source share



All Articles