如何从 Git 存储库的提交历史记录中删除/删除大文件? [英] How to remove/delete a large file from commit history in the Git repository?

查看:37
本文介绍了如何从 Git 存储库的提交历史记录中删除/删除大文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不小心将 DVD-rip 放入网站项目中,然后不小心git commit -a -m ...,而且,zap,repo 膨胀了 2.2 gig.下次我进行了一些编辑,删除了视频文件,并提交了所有内容,但压缩文件仍在存储库中,在历史记录中.

I accidentally dropped a DVD-rip into a website project, then carelessly git commit -a -m ..., and, zap, the repo was bloated by 2.2 gigs. Next time I made some edits, deleted the video file, and committed everything, but the compressed file is still there in the repository, in history.

我知道我可以从这些提交开始分支,并将一个分支变基到另一个分支.但是我应该怎么做才能合并 2 个提交,以便大文件不会显示在历史记录中并在垃圾收集过程中被清除?

I know I can start branches from those commits and rebase one branch onto another. But what should I do to merge the 2 commits so that the big file doesn't show in the history and is cleaned in the garbage collection procedure?

推荐答案

为什么不使用这个简单但功能强大的命令?

Why not use this simple but powerful command?

git filter-branch --tree-filter 'rm -f DVD-rip' HEAD

--tree-filter 选项在每次检出项目后运行指定的命令,然后重新提交结果.在这种情况下,您从每个快照中删除一个名为 DVD-rip 的文件,无论它是否存在.

The --tree-filter option runs the specified command after each checkout of the project and then recommits the results. In this case, you remove a file called DVD-rip from every snapshot, whether it exists or not.

如果您知道哪个提交引入了大文件(比如 35dsa2),您可以将 HEAD 替换为 35dsa2..HEAD 以避免重写太多历史记录,从而避免在您尚未推送的情况下发散提交.@alpha_989 提供的这条评论似乎太重要了,不能忽略.

If you know which commit introduced the huge file (say 35dsa2), you can replace HEAD with 35dsa2..HEAD to avoid rewriting too much history, thus avoiding diverging commits if you haven't pushed yet. This comment courtesy of @alpha_989 seems too important to leave out here.

参见 这个链接.

这篇关于如何从 Git 存储库的提交历史记录中删除/删除大文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆