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
How about git log --pretty=oneline | wc -l
That should count all the commits from the perspective of your current branch.
It might require a relatively recent version of Git, but this works well for me:
git rev-list --count develop..HEAD
This gives me an exact count of commits in the current branch having its base on master.
The command in Peter's answer, git rev-list --count HEAD ^develop
includes many more commits, 678 vs 97 on my current project.
My commit history is linear on this branch, so YMMV, but it gives me the exact answer I wanted, which is "How many commits have I added so far on this feature branch?".
If you are using a UNIX system, you could do
git log|grep "Author"|wc -l
You can also do git log | grep commit | wc -l
and get the result back
Well, the selected answer doesn't work if you forked your branch out of unspecific branch (i.e., not master
or develop
).
Here I offer a another way I am using 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's 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 more analysis, please visit my blog
I like doing git shortlog -s -n --all
. Gives you a "leaderboard" style list of names and number of commits.