如何找到空的 git 提交?

问题描述 投票:0回答:3

我可以使用什么命令来查找 git 存储库中的空提交,即将被

git filter-branch --prune-empty
删除的提交?

git git-filter-branch
3个回答
11
投票

您想要排除无父提交和合并提交,然后查看哪些提交与其父提交具有相同的树。

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

4
投票

作为第一个近似值,以相反的顺序列出所有提交,并记录与之前具有相同树哈希的任何行:

git log --all --reverse --format='%H %t' | while read h t; do
  if [ "$lt" = "$t" ]; then
    echo "$h"
  fi
  lt="$t"
done

您可以通过忽略具有多个父级的任何提交并确认记录的行实际上是之前的子行来改进这一点。


0
投票

这是 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"
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.