What command(s) can I use to find empty commits in a git repository, i.e. the commits that would be removed by git filter-branch --prune-empty
?
Asked
Active
Viewed 3,678 times
9

Eugene Yarmash
- 142,882
- 41
- 325
- 378
3 Answers
11
You'd want to exclude parentless commits and merge commits, then see which ones have the same tree as their parent.
for sha in $(git rev-list --min-parents=1 --max-parents=1 --all)
do
if [ $(git rev-parse ${sha}^{tree}) == $(git rev-parse ${sha}^1^{tree} ) ]
then
echo "${sha} will be pruned"
fi
done

Andrew C
- 13,845
- 6
- 50
- 57
-
1The `==` gives me errors. Switching to `=` works for me (non-bash). – Aaron Wright Jan 27 '22 at 20:08
4
As a first approximation, list all commits in reverse order and log any line that has the same tree hash as the one before:
git log --all --reverse --format='%H %t' | while read h t; do
if [ "$lt" = "$t" ]; then
echo "$h"
fi
lt="$t"
done
You could improve this by ignoring any commit with multiple parents and confirming that the logged line is actually a child of the one before.

Joe
- 29,416
- 12
- 68
- 88
0
This is the solution from the accepted answer in PowerShell:
foreach ($sha in @(git rev-list --min-parents=1 --max-parents=1 --all)) {
if ((git rev-parse "$sha^{tree}") -eq (git rev-parse "$sha^1^{tree}")) {
Write-Output "${sha} will be pruned"
}
}

danN
- 26
- 1
- 8