将git中大型文件删除

背景

在commit时不小心把上百M的数据集加入了, 导致.git文件夹过大,需要将它们从各个分支除去.

探索过程

删除 git 仓库中无用大文件一文中提到使用git verify-pack -v .git/objects/pack/pack-*.idx命令列出各种对象, 但我在pack文件夹内找不到*.idx文件, 查到fan0128说运行git gc可以让文件显示, 但我没尝试这个做法.

看到网上有人说bfg清理工具好用, 于是运行了下马上解决了

解决

解决方案跟随BFG Repo-Cleaner的做即可.

第一次运行时, 反馈说有些commit被保护了, 于是查阅How to remove a protected commit using BFG,得知加上--no-blob-protection参数即可.

E:\codes>java -jar bfg-1.14.0.jar -b 2M mmagnn/.git

Using repo : E:\codes\mmagnn\.git

Scanning packfile for large blobs: 32
Scanning packfile for large blobs completed in 21 ms.
Found 1 blob ids for large blobs - biggest=1007936215 smallest=1007936215
Total size (unpacked)=1007936215
Found 11 objects to protect
Found 7 commit-pointing refs : HEAD, refs/heads/master, refs/heads/pre_sample, ...

Protected commits
-----------------

These are your protected commits, and so their contents will NOT be altered:

 * commit 91a70a5b (protected by 'HEAD')

Cleaning
--------

Found 4 commits
Cleaning commits:       100% (4/4)
Cleaning commits completed in 23 ms.

BFG aborting: No refs to update - no dirty commits found??

第二次运行时, 加上了--no-blob-protection参数, 于是相应的大文件被删除了

E:\codes>java -jar bfg-1.14.0.jar -b 2M mmagnn/.git --no-blob-protection

Using repo : E:\codes\mmagnn\.git

Scanning packfile for large blobs: 32
Scanning packfile for large blobs completed in 20 ms.
Found 1 blob ids for large blobs - biggest=1007936215 smallest=1007936215
Total size (unpacked)=1007936215
Found 0 objects to protect
Found 7 commit-pointing refs : HEAD, refs/heads/master, refs/heads/pre_sample, ...

Protected commits
-----------------

You're not protecting any commits, which means the BFG will modify the contents of even *current* commits.

This isn't recommended - ideally, if your current commits are dirty, you should fix up your working copy and commit that, check that your build still works, and only then run the BFG to clean up your history.

Cleaning
--------

Found 4 commits
Cleaning commits:       100% (4/4)
Cleaning commits completed in 59 ms.

BFG aborting: No refs to update - no dirty commits found??

然后清理一下, 就会发现.git文件夹很小了, git push即可

E:\codes\mmagnn>git reflog expire --expire=now --all && git gc --prune=now --aggressive
Enumerating objects: 26, done.
Counting objects: 100% (26/26), done.
Delta compression using up to 12 threads
Compressing objects: 100% (23/23), done.
Writing objects: 100% (26/26), done.
Total 26 (delta 9), reused 14 (delta 0), pack-reused 0

E:\codes\mmagnn>git push
Everything up-to-date

清理后的文件夹清清爽爽.
.git清理后

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值