GIT彻底删除文件以及历史记录
git不小心将一些其他文件加入了版本管理,可能是大文件,也可能是敏感数据文件。
从分支中删除了文件后提交,依然能过够从历史记录中恢复并查看这个文件,
如果是大文件,那么当其他用户pull的时候,会很耗时,
.git目录是很大的
查看版本库历史:
这时候,需要彻底的删除这个大文件
具体操作如下:
第一步,执行命令:
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch GZBlog/wwwroot/lib/tinymce/plugins/insertCode/images/SQLToolbelt*.exe' --prune-empty --tag-name-filter cat -- --all
GarsonZhang www.infnitee.com
删除文件夹
如果删除的是文件夹,那么需要在 git rm --cached
命令后面添加 -r
命令,表示递归删除(子)文件夹和文件夹下的文件
PowerShell 全选
git filter-branch --force --index-filter 'git rm --cached -r --ignore-unmatch GZBlog/wwwroot/lib/tinymce/plugins/insertCode/images' --prune-empty --tag-name-filter cat -- --all
注意:这里如果路径直接写文件名SQLToolbelt(1).exe,会报错:
查询资料判断为(是关键字,关键字要用\转义
然而路径是支持通配符的,所以这里就直接用了sqltoolbelt*.exe,反正目录中满足这个通配符的就那么一个文件
执行结果如果出现这种情况(*** is unchanged):
代表路径不正确,没有找到文件
正确的执行结果应该是(*** was rewritten):
第二步:以强制覆盖的方式推送你的repo, 命令如下:
$ git push origin master --force --tags
GarsonZhang www.infnitee.com
执行结果:
第三步:清理和回收空间
依次执行命令:
$ git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin $ git reflog expire --expire=now --all $ git gc --prune=now
GarsonZhang www.infnitee.com
结果:
执行过后,再看.git目录大小:恢复正常大小
完成后,其他用户在pull仓库的时候,就不会在耗时的去下载大文件到.git文件夹了
参考:https://www.cnblogs.com/tielemao/p/9973477.html
版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
post YES开发框架