3

不小心误commit超大文件到git,怎么办?

 1 year ago
source link: https://xmanyou.com/how-to-revert-commit-with-large-file/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

27 November 2022 / git

不小心误commit超大文件到git,怎么办?

github 默认允许push推送的最大文件为100M,但是有时候使用一些外部插件时,依然会不小心误commit一些大文件,然后在推送到github时,就会遇到错误提示:

remote: error: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com

意思是,检测到了大文件。

最糟糕的是,这个错误会导致本地所有未推送到远程的commit记录都无法正常推送。

首先需要提的是错误做法:删除该文件,然后再次提交commit。

这是一个标准的错误做法,因为删除文件后commit,并不会删除之前的提交历史,所以此时推送的话,会进行以下操作:

  • 提交大文件
  • ...其他操作
  • 删除大文件

但是,在推送大文件时,依然会遇到错误,导致后续的删除无法进行。

那么,如何正确删除或者修改操作记录呢?

标准且直接的修改commit的方法是,使用以下命令:

  • git log 查看commit历史记录,用于查找目标记录
  • git rebase -i 修改错误的提交记录
  • git rm 删除大文件
  • git commit --amend -C HEAD 提交修改过的commit记录
  • git rebase --continue 确认完整的修改历史

顺利的话,最后你就可以正常进行git push了。

具体的可以参考以下教程:

这一系列git操作,对于不熟悉的人来说,是很繁琐的,而且很容易出错,一旦出错,就要进行二次修正。

那么,有没有什么简便的方法呢?

更便捷且不易出错的方法是,不直接去修改commit历史记录,而是先回滚,然后重新提交。

具体的操作步骤:

  • 备份当前的所有文件,凡事先备份
  • 回滚到出错之前的版本,如果不备份直接回滚,会之前所有的修改都丢失
  • 将备份的文件覆盖回滚后的版本
  • 将大文件添加到.gitignore忽略列表

副作用
任何便捷的方法几乎都有副作用,这次也不例外:虽然你不会丢失任何文件,但是会丢失文件的版本记录,导致无法按需commit。
如果需要这么做,那么你可以考虑依次回滚,然后依次提交,或者使用标准的rebase+amend方法。

阿斌

Read more posts by this author.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK