github删除历史大文件

github中提交了大文件如何删除

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# 查看 git 文件对象大小
git count-objects -v
 
# 列出体积最大的 top10
git verify-pack -v .git/objects/pack/pack-*.idx | sort -k 3 -g | tail -10
# 4bdc96af2540a6002a60e0f8026ac6edb8ceec7f blob   2104539 2103812 68220097
# bcaab23744d18bf7d80e2914a4820e199954b7a7 blob   2681512 2679930 52983447
# d0dd82d41145885996285a67d119862256e09b3d blob   2689821 2685468 70323909
 
# 查看具体文件名称
git rev-list --objects --all | grep <commit-id>
# 栗子: git rev-list --objects --all | grep bcc72e78c11b79c3744ae6b0cc36806b6d02dde5
# 结果: 4bdc96af2540a6002a60e0f8026ac6edb8ceec7f public/images/background/bg4.jpg
 
 
# 清除该文件
git filter-branch --force --index-filter 'git rm --cached -r --ignore-unmatch file_you_want_to_delete' --prune-empty --tag-name-filter cat -- --all
# 栗子:git filter-branch --force --index-filter "git rm --cached --ignore-unmatch 'public/images/background/bg4.jpg'"  --prune-empty --tag-name-filter cat -- --all
 
# 清空 git 历史
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
# 如果这一步有权限不足的问题,需要手动到github上将master分支的保护去掉
git push origin master --force
# 清理远程仓库
git remote prune origin

如果git push报错如下

hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., ‘git pull …’) before pushing again. hint: See the ‘Note about fast-forwards’ in ‘git push —help’ for details. ————————————————

执行如下命令(修改成你对应的分支,我这里是dev)

1
git pull origin dev --allow-unrelated-histories
Licensed under CC BY-NC-SA 4.0
最后更新于 Apr 11, 2025 07:19 UTC
comments powered by Disqus
Built with Hugo
主题 StackJimmy 设计
Caret Up