git从整个历史记录中删除所有已删除的文件

时间:2012-12-18 10:37:31

标签: git bash git-bash

我想知道是否有人有更高效,更聪明的方法来做到这一点。循环结构要求通过读取每个提交,从每次提交中清除每个已删除的文件。有很多提交,这需要很长时间。

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

4 个答案:

答案 0 :(得分:3)

这似乎一次删除一个文件 考虑git rm可以删除多个文件。

因此,一个优化是构建该列表并调用filter-branch一次 您可以在“Proper way to remove unwanted files with git filter-branch without git rm failing”中看到该方法的一个示例。

答案 1 :(得分:1)

对于可能正在寻找相同答案的其他人,我使用the other thread中链接的建议修改了上面的原始命令,并创建了该命令,该命令似乎对我有用。

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'

答案 2 :(得分:1)

仅需一点点改进即可支持递归删除

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'

答案 3 :(得分:0)

改进了避免使用'导致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"

相关问题