一不小心,仓库内提交了无用大文件该如何处理

81 阅读2分钟

Flutter使用melos插件化已有项目时,一不小心把apps下项目的build里的给提交上去。 又有新的提交,(⊙o⊙)…脑壳大 【git超限】

首先如果是Ubuntu

利用git-filter-repo来删除非必要的错误提交大文件,并保留所有commit

报错如下:

remote: the blob a08f8221a8328f421f474b999722e3ba81546d71 size 1 GiB exceeded limit 500 MiB remote: 推送的 blob 对象 a08f8221a8328f421f474b999722e3ba81546d71 大小 1 GiB 超过了单个文件大小限制 500 MiB。

方法一:

wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
pip3 install git-filter-repo

方法二:

wget https://raw.githubusercontent.com/newren/git-filter-repo/main/git-filter-repo
chmod +x git-filter-repo
sudo mv git-filter-repo /usr/local/bin/

上面我试了,但是……不大行,我就直接copy, 链接raw.githubusercontent.com/newren/git-…

1、复制到文件里

nano ~/git-filter-repo

2、加上执行权限并移动到PATH

chmod +x ~/git-filter-repo
sudo mv ~/git-filter-repo /usr/local/bin/

3、测试方法:

git filter-repo

如果终端输出

No arguments specified.

很好,现在你已经安装好git-filter-repo

step1 备份当前仓库

cp -r 目录 目录_backup

step2 运行filter-repo

git filter-repo --path build/ --invert-paths --force

你会发现remote没有了 现在需要重新添加remote

git remote add origin 远端仓库.git
# 检查一下是否成功
git remote -v

step3 检查是否还有大文件残留

git rev-list --objects --all | sort -k 2 | git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize:disk) %(rest)' | sort -k 3 -n | tail -n 20

可以根据情况删除

git filter-repo --force \
  --path apps/horizontal_version/assets/army/videos/ --invert-paths \
  --path apps/horizontal_version/sdk_libs/model/ --invert-paths \
  --path apps/horizontal_version/build/linux/arm64/debug/bundle/lib/libcef.so --invert-paths \
  --path apps/horizontal_version/build/linux/arm64/release/bundle/lib/libcef.so --invert-paths

setp4 接下来就是pull push

Windows

报错如下

the blob a08f8221a8328f421f474b999722e3ba81546d71 size 1 GiB exceeded limit 500 MiB

用下面命令查找

git rev-list --all | ForEach-Object {
    git ls-tree -r $_
} | Select-String "a08f8221a8328f421f474b999722e3ba81546d71"

发现有个libcef.so文件不小心提交上去了

step1 用 BFG

下载 BFG Repo Cleaner rtyley.github.io/bfg-repo-cl…

step2 删除目标大文件

java -jar ../bfg-1.14.0.jar --delete-files libcef.so