如何获取大文件的pathspec执行git rm?Git推送失败求助
解决Git推送因大文件bootstrap.zip失败的问题
第一步:定位文件在Git历史中的正确路径
你遇到的pathspec错误要么是拼写失误(你之前输入的bootsrtap.zip少了字母t),要么是文件在历史提交里的路径和当前不一致。执行以下命令找出准确路径:
- 列出所有包含
bootstrap.zip的提交:
git log --all --pretty=format:"%H %s" -- | while read commit msg; do git ls-tree --name-only -r "$commit" | grep -q "bootstrap\.zip" && echo "$commit $msg" done
- 针对输出的提交哈希,查看该提交中文件的完整路径:
git ls-tree --name-only -r <提交哈希>
例如替换为git ls-tree --name-only -r a1b2c3d,就能得到文件的准确路径(比如src/bootstrap.zip或根目录的bootstrap.zip)。
第二步:彻底移除历史中的大文件
官方已弃用git filter-branch(易出现遗漏或错误),改用更可靠的git filter-repo:
先安装
git filter-repo:- Debian/Ubuntu:
sudo apt install git-filter-repo - macOS:
brew install git-filter-repo - 通用方式:
pip install git-filter-repo
- Debian/Ubuntu:
用找到的完整路径执行移除命令:
git filter-repo --path <文件完整路径> --invert-paths
如果文件在根目录,直接执行:
git filter-repo --path bootstrap.zip --invert-paths
第三步:更新索引并强制推送
- 若本地索引仍有该文件记录,执行:
git rm --cached bootstrap.zip
(注意拼写正确:bootstrap.zip,不要写成bootsrtap.zip)
- 强制推送修改后的历史(因为改写了提交历史,必须用
--force):
git push origin --force --all git push origin --force --tags # 项目有标签时执行此命令
验证
推送完成后再次执行git push确认无错误,同时检查.gitignore中已正确添加bootstrap.zip(路径需匹配实际位置)。
内容的提问来源于stack exchange,提问作者upcoming
相关产品推荐
相关产品推荐

