我想知道是否有人有更有效、更智能的方法来做到这一点。循环结构要求通过读取每个提交从每个提交中删除每个已删除的文件。对于许多提交,这需要很长时间。
git log --pretty=format: --name-status | grep -i ^D | cut -f2- | sort -u | xargs -I {} git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch {}' HEAD
这似乎一次删除每个文件。
考虑到git rm
可能需要删除多个文件。
因此,一种优化是构建该列表并调用一次过滤器分支。您可以在“正确删除不需要的文件而不失败
”
中查看该方法的一个示例。git filter-branch
git rm
对于可能正在寻找相同答案的其他人,我使用另一个线程中链接的建议修改了上面的原始命令,并创建了这个似乎对我有用的命令。
git filter-branch --prune-empty --index-filter 'git log --pretty=format: --name-status | grep -i ^D | cut -f2- | sort -u | xargs git rm --cached --ignore-unmatch DoesNotExistInMyProject'
支持递归删除的小改进
git filter-branch --prune-empty --index-filter 'git log --pretty=format: --name-status | grep -i ^D | cut -f2- | sort -u | xargs git rm -r --cached --ignore-unmatch DoesNotExistInMyProject'
改进了命令以避免带有 ' 的文件名导致 xargs 出错
echo "/!\ This file contains a single quote ' and it's gonna bug with xargs, so it's excluded from the filter operation" ; bash -c "git log --pretty=format: --name-status | grep -i ^D " | grep \'
git filter-branch -f --prune-empty --index-filter "git log --pretty=format: --name-status | grep -i ^D | grep -v \' | cut -f2- | sort -u | xargs git rm -r --cached --ignore-unmatch DoesNotExistInMyProject"