3

从 git 版本库中删除文件的历史记录

 3 years ago
source link: https://zhiqiang.org/coding/remove-file-from-git-history.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

从 git 版本库中删除文件的历史记录

作者: 张志强

, 发表于 2016-09-11

, 共 389 字 , 共阅读 71 次

有时候不小心把一些大文件 commit 到了本地版本库,但往网络版本库同步时,因为文件过大被拒绝。如果在上传前,本地有多次提交,此时再在当前版本里删除这些大文件还不够,还需要把历史记录全删掉。效果要跟从来没提交过这些文件一样。

# 删除匹配文件
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.zip'

# 回收空间
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
du .git -lsh

# 同步到网络
git push origin master --force

Q. E. D.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK