彻底删除git中的较大文件(包括历史提交记录)

场景

适用于从一个git项目中,将体积较大的资源彻底从git中删除,包括历史提交记录。

如果仅仅在目录中删除一个文件是不够的,只要在提交记录中有这个文件,那么 .git 中就会有这个文件的信息。

filter-branch 可以强制修改提交信息,将某个文件的历史提交痕迹也抹去,就像从来没有过这个文件一样。

做法

0、确保本地仓库是最新版本。

1、在项目根目录下运行

git rev-list --all | xargs -rL1 git ls-tree -r --long | sort -uk3 | sort -rnk4 | head -10

列出所有仓库中的对象(包括SHA值、大小、路径等),并按照大小降序排列,列出TOP 10。

$ git rev-list --all | xargs -rL1 git ls-tree -r --long | sort -uk3 | sort -rnk4 | head -10
100644 blob 71ac1de8ee83566cca68f69d54acd82b9abf607d 7701044    "attr_matching/\346\240\207\346\263\250\346\226\207\344\273\266Win\346\216\222\345\272\2171-54000.xlsx"
100644 blob d610f01bc315becaaa1c6d04772689d80ad4d010 7532106    "attr_matching/\346\240\207\346\263\250\346\226\207\344\273\266Win\346\216\222\345\272\2171-58000.xlsx"
100644 blob 72b8fd2eeb1c951a7f680e6f4031fa57d643ebe1 5717635    personal_label_project/personal_label_ocr/result/matrix.csv
100644 blob 89363c480afb229b5df4e2f6a6a951fdebca5ce2 5501428    cloth_attribute_classification/nohup_1103.out
100644 blob 7cdba6a4e584a49dcab693111a90f2bdb0030a3c 5392316    sheos_category_classification/nohup_gpu_category_33_0.out
100644 blob 84149a46904f87495df4896698ca5dd6fb53bdf3 5226094    cloth_attribute_classification/nohup_1105.out
100644 blob c77db27ae9fbb5450586daa3c9622766d7a4a9bc 5151535    attr_matching/OCR_result_correct.txt
100644 blob 58e4647427c443a44fbc66d584f6bda21b7c9036 2785190    image_check/out_tmp/nohup_5w_test_sheos.out
100644 blob 58e4647427c443a44fbc66d584f6bda21b7c9036 2785190    image_check/out_tmp/nohup_50000_sheos.out
100644 blob 58e4647427c443a44fbc66d584f6bda21b7c9036 2785190    image_check/nohup_5w.out

2、根据最大文件的路径 {filepath},修改此文件的commit历史:

git filter-branch --tree-filter "rm -f {filepath}" -- --all

3、强制提交到远程分支:

git push -f --all

4、完成。可以重新下载项目,列出所有仓库中的对象,看是否还存在刚刚删除的文件。

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值