git,

How to get Git commit count

Aug 21, 2021 · 1 min read · 8 views · Post a comment

In today’s tutorial we are going to see how can we get the Git commit count of empty and non-empty commits. So, here’s the neat commands you might need.

Prerequisites

  • Git

Solution

Commit count across all branches:

git rev-list --all --count

Commit count for a revision (HEAD, master, a commit hash):

git rev-list --count <revision>

Count empty commits:

git rev-list HEAD | while read commitHash; do
    if [ $(git diff-tree --name-status --no-commit-id $commitHash | wc -l) -eq 0 ]; then
        echo '1'
    fi;
done | wc -l

Count non-empty commits:

git rev-list HEAD | while read commitHash; do
    if [ $(git diff-tree --name-status --no-commit-id $commitHash | wc -l) -gt 0 ]; then
        echo '1'
    fi;
done | wc -l

Conclusion

You could do the same via the GitHub API as well. To find more neat Git commands and hacks, browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.

git