github push失败,删除git历史中的大文件

转移 repo 的时候,直接 push 会报错文件大小受到限制,但是这个文件只存在于 commit history,并不在实际文件中。

1. 找出 history 中的大文件

参考:https://stackoverflow.com/questions/10622179/how-to-find-identify-large-commits-in-git-history

1
2
3
4
5
6
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sed -n 's/^blob //p' |
sort --numeric-sort --key=2 |
cut -c 1-12,41- |
$(command -v gnumfmt || echo numfmt) --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest

2. 使用 BFG 删除

1
bfg -D <文件名>

参考:https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/removing-sensitive-data-from-a-repository

3. 清理缓存

1
git reflog expire --expire=now --all && git gc --prune=now --aggressive

4. 强制推上 GitHub

1
git push -f

注意:强制推送会改写远程历史,操作前请确认没有其他人依赖该分支。

Notice: 正常情况下,这里会有一个基于utteranc.es的留言系统,如果看不到,可能要想想办法才能看到。

Powered by Hexo and Hexo-theme-hiker

Copyright © 2012 - 2026 tiaobug.com All Rights Reserved.

鲁ICP备2024124237号-1