从git repo和提交历史中递归删除所有二进制文件

时间:2013-07-02 06:08:35

标签: git git-rewrite-history

我在从git commit history中删除大型二进制文件时已经阅读了few different threads,但我的问题略有不同。因此,我的问题是要理解并确认步骤 -

我的git repo是~/foo。我想从repo中的一个目录中删除所有* .jpg,* .png,* .mp4,* .ogv(等等),特别是从~/foo/public/data删除。

步骤1.删除文件

~/foo/data > find -E . -regex ".*\.(jpg|png|mp4|m4v|ogv|webm)" \
    -exec git filter-branch --force --index-filter \
    'git rm --cached --ignore-unmatch {}' \
    --prune-empty --tag-name-filter cat -- --all \;

步骤2.将二进制文件扩展名添加到.gitignore并提交.gitignore

~/foo/data > cd ..
~/foo > git add .gitignore
~/foo > git commit -m "added binary files to .gitignore"

步骤3.推送所有内容

~/foo > git push origin master --force

我在上面的正确轨道上吗?我想在切一次之前测量两次,所以说。

更新:嗯,上面给出了错误

You need to run this command from the toplevel of the working tree.
You need to run this command from the toplevel of the working tree.
..

所以我把树上升到顶层并重新运行命令,这一切都有效。

1 个答案:

答案 0 :(得分:7)

这个过程似乎正确。

您还可以使用tool like bfg repo cleaner测试清洁流程,例如this answer

java -jar bfg.jar --delete-files *.{jpg,png,mp4,m4v,ogv,webm} ${bare-repo-dir};

(除了BFG确保它不会删除最新提交中的任何内容,因此您需要删除当前索引中的那些文件并进行“干净”提交。所有其他以前的提交都将由BFG清除)