列出所有没有更改文件的git提交

时间:2017-11-13 23:33:46

标签: git

我正在使用一个开源存储库,它似乎是从一个组合的git repo或其他类型的VCS中复制过来的。在repo中有大量的提交,其中大多数都没有更改文件:

Almost 40 thousand commits

Most of them have changed zero files

列出所有未更改文件的提交,计算它们并将其从本地git repo中删除的最佳方法是什么?

编辑:我专门寻找一种方法来评估问题的严重程度,然后运行Remove empty commits in git

中引用的filter-branch等耗时的破坏性命令

1 个答案:

答案 0 :(得分:0)

所以事实证明,我在Google中缺少的关键字是#34;空的" (正在搜索"删除没有文件更改的提交"等)

列出更改的提交(提交):

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

列表提交更改,文件已更改(空提交):

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

计算提交

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

计算非空提交

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

最后,根据@JKillian的建议,使用git filter-branch删除回购邮件中的所有空提交:

git filter-branch --tag-name-filter cat --commit-filter 'git_commit_non_empty_tree "$@"' -- --all

filter-branch上的文档,特别是--commit-filter

https://git-scm.com/docs/git-filter-branch#git-filter-branch---commit-filterltcommandgt