Git提交含大文件无法推送:如何移除多提交文件及正确用LFS?
报错信息:
Enumerating objects: 863, done.
Counting objects: 100% (863/863), done.
Delta compression using up to 8 threads
Compressing objects: 100% (758/758), done.
Writing objects: 100% (834/834), 245.10 MiB | 2.91 MiB/s, done.
Total 834 (delta 111), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (111/111), completed with 7 local objects.
remote: error: Trace: 3c5c28a3d91ced
remote: error: See https://gh.io/lfs for more information.
remote: error: File TI - Code/simplelink_msp432p4_sdk_3_40_01_02.run is 214.80 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 - https://git-lfs.github.com.
To https://github.com/YYYYY/Autonomous-Craft-2D.git
! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://github.com/YYYYY/Autonomous-Craft-2D.git'
方案一:正确使用Git LFS托管大文件
你之前只跟踪了.pdf文件,但报错里的大文件是.run格式,这是核心问题,按以下步骤操作:
- 修正LFS跟踪规则,包含目标大文件类型:
git lfs track "*.run" git lfs track "*.pdf" # 保留原有PDF文件的跟踪 git add .gitattributes git commit -m "Update LFS tracking for .run and .pdf files" - 将历史提交中的大文件替换为LFS指针(遍历所有提交处理):
git lfs migrate import --include="*.run" --include="*.pdf" --everything - 强制推送修改后的提交历史:
注意:如果团队其他成员已经拉取过这些提交,他们需要重新拉取并同步本地分支,避免冲突。git push --force origin main
方案二:彻底从所有提交中移除大文件
如果不需要保留该大文件,可彻底清理所有提交中的记录:
- 确保安装了
git-filter-repo(多数新版Git已集成,若缺失可通过系统包管理器安装) - 执行移除命令,指定要删除的文件路径:
该命令会遍历所有提交,仅删除指定文件,其他提交内容不受影响。git filter-repo --path "TI - Code/simplelink_msp432p4_sdk_3_40_01_02.run" --invert-paths - 强制推送清理后的历史:
git push --force origin main - 清理本地冗余Git对象,释放磁盘空间:
git reflog expire --expire=now --all git gc --prune=now --aggressive
内容的提问来源于stack exchange,提问作者Kasriel Lewis

