Git不小心commit了很大的文件怎么办?

前言:

  大家使用git不知道有没有一个习惯,基本就是     一套 df 二连 素质三连:

  1 git add .    2 git commit -m  3 git pull  4  git  push

拉闸,不小心commit了超过10M的文件,拉闸。 出现了一个错误。 

The size of file ‘xxx‘ has exceeded the upper limited size (10 MB) in commit

意思就是我们commit超过10MB的文件无法提交。 (git 可以修改这个上限,自己去百度,好像上限是50MB)

这时候不小心将一个很大的文件添加到库中,即使删除,记录中还是保存了这个文件。以后不管是拷贝,还是push/pull都比较麻烦,尤其是在做CI时的耗时很痛苦。

方法:

1、若知道你的大文件是什么类型的,比如.gz或者.jar等,可以删除匹配xxx的所有文件:

比如我很明显就提示我是xxx.jar 太大了。 因为我打了整个项目的jar包,接近100MB肯定大

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.gz' --prune-empty --tag-name-filter cat -- --all

2、若不知道,想查看下目前git库中有哪些大文件,可以用底层命令verify-pack识别出大对象:

git verify-pack -v .git/objects/pack/pack-8eaeb...9e.idx | sort -k 3 -n | tail -3

然后根据对象id查询对象的名称:

git rev-list --objects --all | grep 185ab8d

然后删除

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch 对象path' --prune-empty --tag-name-filter cat -- --all

3、释放空间

rm -rf .git/refs/original/ 
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now

4、把修改强制推送到远端

git push origin --force --all

我的话 是知道哪个jar了,所以我直接就使用第一个方法 匹配删除了

其实 git filter-branch 是一个很强的命令。

猜你喜欢

转载自blog.csdn.net/xmh594603296/article/details/83987271