从Git历史中彻底删除大文件

描述

1
2
3
4
5
remote: Resolving deltas: 100% (23/23), completed with 17 local objects.
remote: error: Trace: 4836afb0ea0a96bb85e61a8ae818673e2f110a05c0da9e6813f8fa0c34ba2816
remote: error: See https://gh.io/lfs for more information.
remote: error: File pdsv3.json is 184.08 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: GH001: Large files detected. You may want to try Git Large File Storage

方案

  1. 备份当前代码(防止操作失误)

  2. 安装git-filter-repo

1
pip install git-filter-repo
  1. 从所有Git历史中删除文件
1
git filter-repo --path pdsv3.json --invert-paths
  1. 强制推送到远程仓库
1
2
3
git push origin --force main 
git push origin --force --all  # 强制推送所有分支
git push origin --force --tags  # 强制推送标签(如有必要)
  1. 清理本地仓库残留
1
git gc --prune=now 

Git LFS(Large File Storage)

方案

  1. 安装git LFS
1
2
3
4
5
6
7
8
# 适用于 macOS/Linux
brew install git-lfs

# 或通过官方脚本
curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash
sudo apt-get install git-lfs

# Windows 用户可从官网下载安装包:https://git-lfs.com/
1
git lfs install
  1. 用 LFS 跟踪大文件
1
git lfs track "pdsv3.json"
  1. 提交并推送
1
2
3
git add pdsv3.json
git commit -m "Track pdsv3.json with Git LFS"
git push --force origin main
  1. 验证文件是否被 LFS 管理
1
git lfs ls-files 

Could not read from remote repository

问题描述

1
2
3
4
5
6
git push origin --force --all
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

方案

  1. 检查当前远程仓库配置
1
git remote -v
  1. 添加/修正远程仓库

情况 1:从未配置过远程仓库 如果 git remote -v 无输出,手动添加 origin:

1
git remote add origin https://github.com/{username}/{repo-name}.git

情况 2:origin URL 错误 删除旧 origin 并重新添加:

1
2
git remote remove origin  # 删除旧配置
git remote add origin 正确的仓库URL  # 重新添加