场景:git不小心上传了一个大文件,导致无法上传repo到Github,需要把这个大文件的git历史全部删除,仅仅靠git rm -r --cached --ignore-unmatch.gitignore只会取消文件跟踪,git历史中依然有文件的提交历史,占用空间,需要想办法彻底删除,否则无法push

git rm -r --cached --ignore-unmatch

  • 这个只会取消文件跟踪,但是文件依然会存在于之前commit历史中

git filter-branch

删除指定文件/文件夹

1
2
3
4
5
git rm -r --cached --ignore-unmatch 文件1.md
git commit -m "🔥"
git filter-branch --force --index-filter \
'git rm -r --cached --ignore-unmatch 文件1.md' \
--prune-empty --tag-name-filter cat -- --all

注意,需要先运行git rm -r --cached --ignore-unmatch,并git commit,否则git 这个命令会把文件和文件历史全部删除,不保留工作区的文件

git-filter-repo🙂

参考:git-filter-repo/Documentation/converting-from-filter-branch.md

安装filter-repo

1
pip install  git-filter-repo

删除历史操作

1
2
3
4
5
6
7
git rm -r --cached --ignore-unmatch 文件1.md
git commit -m "🔥"
git filter-repo --invert-paths --path 文件1.md

# 会丢失远端操作需要重新绑定remote 仓库,需要重新绑定,并强力删除
git remote add origin https://github.com/Achuan-2/vscode-git--.git
git push origin --all --force