So it turns out the keyword I was missing in Google was "empty" (was searching for "remove commits with no files changes", etc)
List commits that have no changes (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 $commitHash
fi;
done
List commits that have changes, and files changed (non empty commits):
git rev-list HEAD | while read commitHash; do
git diff-tree --name-status $commitHash
done
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
And finally, as per @JKillian's suggestion, remove all empty commits from the repo using git filter-branch
:
git filter-branch --tag-name-filter cat --commit-filter 'git_commit_non_empty_tree "$@"' -- --all
Documentation on filter-branch
, specifically --commit-filter
:
https://git-scm.com/docs/git-filter-branch#git-filter-branch---commit-filterltcommandgt