git之从整个版本中彻底删除文件)

1.背景

  最近将托管在gitee.com上代码放到github.com上,通过github网站从gitee.com直接导入github提示失败,具体请原因位置。之后从命令上手动导入,出现了一下问题记录下来,以供参考。

2.导入

2.1 删除远程仓库gitee.com

$ git remote rm origin

2.2 增加远程仓库github.com

$ git remote add origin https://github.com/flysnow010/flyscp.git

说明:

  • 远程仓库flyscp.git是在github创建的一个空仓库

2.3 切换到main分支

$ git branch -M main

2.4 推送到远程分支

$ git push -u origin main

就在推送到最后出问题,问题如下:

$ git push -u -f origin main
Enumerating objects: 2720, done.
Counting objects: 100% (2720/2720), done.
Delta compression using up to 16 threads
Compressing objects: 100% (772/772), done.
Writing objects: 100% (2720/2720), 138.06 MiB | 4.47 MiB/s, done.
Total 2720 (delta 1916), reused 2720 (delta 1916), pack-reused 0
remote: Resolving deltas: 100% (1916/1916), done.
remote: error: Trace: 5cf2bc7a226754a731ec4fa76e04b16f1a5384db29e8967f7cbb6c0b6c4ba850
remote: error: See https://gh.io/lfs for more information.
remote: error: File doc/windows-win32-shell.pdf is 123.69 MB; this exceeds GitHub's file size limit of 100.00 MB
remote: error: File doc/windows-win32-shell_en.pdf is 116.72 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/flysnow010/flyscp.git
 ! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://github.com/flysnow010/flyscp.git'

从错误去信息看是doc目录下有两个文件windows-win32-shell.pdf和doc/windows-win32-shell_en.pdf大小超过100MB。
通过如下命令删除两个文件:

git rm doc/windows-win32-shell.pdf
git rm doc/windows-win32-shell_en.pdf
git commit -m "rm big file"

再次提交时还是出现上述问题。

3问题解决

3.1 尝试1

怀疑本地有缓冲,重新下载代码删除两个文件后,再上传:

$ git clone https://gitee.com/flysnow_1/flyscp.git
$ cd flyscp
$ git remote rm origin
$ git remote add origin https://github.com/flysnow010/flyscp.git
$ git branch -M main
$ git push -u origin main

问题依旧。

3.2 尝试2

怀疑远程有缓存,增加选项-f强制覆盖远程。

$ git push -u -f origin main

问题依旧。

3.3 尝试3

最后考虑到,随然再当前版本中删除两个大文件,但历史版本中还存在这两个文件,看来需要从历史版本中删除这个两文件。
从历史版本中删除文件:

$ git filter-branch --force --index-filter \
>   "git rm --cached --ignore-unmatch doc/windows-win32-shell.pdf" \
>   --prune-empty --tag-name-filter cat -- --all

运行结果:

WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.

Proceeding with filter-branch...

Rewrite 36a840451c2057f8eddef65f653d74fa5e59712d (67/406) (36 seconds passed, remaining 182 predicted)    rm 'doc/windows-win32-shell.pdf'
Rewrite 463481753d6d19e5c59efcd12d7648e83476f428 (69/406) (37 seconds passed, remaining 180 predicted)    rm 'doc/windows-win32-shell.pdf'
Rewrite 6190fd4f218d742d231dfe46f6e2c6fb069c43da (69/406) (37 seconds passed, remaining 180 predicted)    rm 'doc/windows-win32-shell.pdf'
Rewrite 711e116b1a581df68bd53f4354e0642f08112f54 (71/406) (38 seconds passed, remaining 179 predicted)    rm 'doc/windows-win32-shell.pdf'
Rewrite 9ef306ab2d0af696fde3addbfa2f6b06566bb1ad (71/406) (38 seconds passed, remaining 179 predicted)    rm 'doc/windows-win32-shell.pdf'
Rewrite 97e232f44c5014cb174342a4a3f0309affef99c1 (73/406) (39 seconds passed, remaining 177 predicted)    rm 'doc/windows-win32-shell.pdf'
Rewrite cc485fdc99e222822ffdd27b0b1a8dc170e1f473 (73/406) (39 seconds passed, remaining 177 predicted)    rm 'doc/windows-win32-shell.pdf'
Rewrite ff0c50789afc7321cd9683a2fd74144ddfbe6a9c (75/406) (40 seconds passed, remaining 176 predicted)    rm 'doc/windows-win32-shell.pdf'
Rewrite 11c08e799c3308886dddf085be5cc6648e598421 (75/406) (40 seconds passed, remaining 176 predicted)    rm 'doc/windows-win32-shell.pdf'
...
Rewrite 4bef6e154efa71ae30745eebde31dce12fc42980 (405/406) (226 seconds passed, remaining 0 predicted)
Ref 'refs/heads/main' was rewritten

看来使用这个命令导致历史库被重新修改了

用同样命令删除另一个文件:

$ git filter-branch --force --index-filter \
  "git rm --cached --ignore-unmatch doc/windows-win32-shell_en.pdf" \
  --prune-empty --tag-name-filter cat -- --all

3.4 问题解决

$ git push -u origin main

运行结果:

$ push -u -f origin main

Enumerating objects: 2712, done.
Counting objects: 100% (2712/2712), done.
Delta compression using up to 16 threads
Compressing objects: 100% (1096/1096), done.
Writing objects: 100% (2712/2712), 65.27 MiB | 4.23 MiB/s, done.
Total 2712 (delta 1908), reused 1972 (delta 1584), pack-reused 0
remote: Resolving deltas: 100% (1908/1908), done.
To https://github.com/flysnow010/flyscp.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

代码成功推送上去了。

4 总结

  • github对文件大小有限制不能超过100M.
  • 从历史版本中删除文件会导致设计到整个历史版本信息被修改,慎重使用。
  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysnow010

你的鼓励就是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值