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

pun*_*ish 9 git git-rewrite-history

我从git commit history中删除了大量二进制文件的几个 不同的 线程,但我的问题有点不同.因此,我在这里要了解并确认步骤 -

我的git回购是~/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 \;
Run Code Online (Sandbox Code Playgroud)

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

~/foo/data > cd ..
~/foo > git add .gitignore
~/foo > git commit -m "added binary files to .gitignore"
Run Code Online (Sandbox Code Playgroud)

第3步.推动一切

~/foo > git push origin master --force
Run Code Online (Sandbox Code Playgroud)

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

更新:嗯,上面给我的错误

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.
..
Run Code Online (Sandbox Code Playgroud)

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

Von*_*onC 7

这个过程似乎是对的.

你也可以用一个测试你的清洁工艺像BFG回购清洁工具,在这个答案:

java -jar bfg.jar --delete-files *.{jpg,png,mp4,m4v,ogv,webm} ${bare-repo-dir};
Run Code Online (Sandbox Code Playgroud)

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

  • BFG很可能是一个很好的工具(免责声明:我是BFG的创建者) - 我只是想澄清它与`git-filter-branch`做类似的工作,所以它可能会_replace_脚本在第1步(而不是'测试'它).然而,BFG作用于整个仓库,目前不能限制在单个文件夹路径,如`〜/ foo/public/data`.如果具有这些扩展名的文件在repo中的其他位置不存在,那么这不是问题.或者,如果它们*存在但存在受保护的提交(例如,您的`HEAD`提交),那么它们也不会被删除. (3认同)