git filter-branch
将之前提交过的文件从版本中删除
git filter-branch --help
解释
git-filter-branch - Rewrite branches
语法
git filter-branch
[--env-filter <command>]
[--tree-filter <command>]
[--index-filter <command>]
[--parent-filter <command>]
[--msg-filter <command>]
[--commit-filter <command>]
[--subdirectory-filter <directory>]
[--prune-empty]
[--original <namespace>]
[-f | --force]
[--] [<rev-list options>…]
示例
git filter-branch --index-filter 'git rm --cached --ignore-unmatch b.c' -f
–tree-filter表示历史提交。
–index-filter
–msg-filter表示修改提交信息,原提交信息从标准输入读入,新提交信息输出到标准输出。
-f是忽略备份。不加这个选项第二次运行这个命令时会出错,意思是 git 上次做了备份,现在再要运行的话得处理掉上次的备份。
问题
$ du -hs
虽然原文件已经删除了,但查看文件大小还是包括删除的文件,
执行以下git语句则节省大小
git ls-remote .
rm -rf .git/logs
git reflog --all
git prune
git gc
du -hs
在window 命令行行执行有问题,建议在bash.exe中执行,
不要打开多个bash,会出现没权限的问题
可以用一段脚本执行,保存成.bat格式的任意文件名
放在与git项目同级的目录下,demo换成git项目的目录,
因为命令行下运行filter-branch有问题,两句还不能直接加上,高手,知道的可以评论
cd demo
#bash.exe
#git filter-branch --index-filter 'git rm --cached --ignore-unmatch demo2.txt' -f
#git filter-branch --tree-filter 'git rm --cached --ignore-unmatch demo2.txt' -f
git ls-remote .
rm -rf .git/logs
git reflog --all
git prune
git gc
du -hs
参考
http://weimenlove.blog.163.com/blog/static/17775473201322395251716/