git,

How to list Git empty commits

Aug 26, 2022 · 1 min read · Post a comment

Pushing empty commits or in general with no files changed is mostly done when triggering a deployment (CI/CD) pipeline, speaking from my personal experience for sure. However, if you want to get rid of them, you need to know how to list them first. Here’s some useful commands to do so.

Prerequisites

  • Git

Solution

To list empty commits only, go to your current Git repo and run the following command:

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

To list non-empty commits only, run:

git rev-list HEAD | while read commitHash; do
    git diff-tree --name-status $commitHash
done

Conclusion

Related posts:

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