github仓库中删除历史大文件

臭大佬 2020-07-31 17:41:30 2608
Git 
简介 github仓库中删除历史大文件

问题

如果git中提交了大文件,而且保存到了版本库中,那在下载或者克隆git包的时候,速度会非常慢。再加上github在国内访问本来就很慢,可能会导致包无法下载(克隆)。为了提升下载(克隆)速度,可以永久的删除这些文件(包括该文件的历史记录),操作方法如下;

方法

# 查看 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)

git pull origin dev --allow-unrelated-histories