我不小心把一个dvd光盘放到了一个网站项目中,然后不小心提交-a -m…而且,快,回购膨胀了2.2 g。下次我做了一些编辑,删除了视频文件,并提交了所有内容,但压缩文件仍然在存储库中,在历史中。
我知道我可以从这些提交中启动分支,并将一个分支重置到另一个分支上。但是我应该怎么做才能合并两次提交,使大文件不显示在历史记录中,并在垃圾收集过程中被清理?
我不小心把一个dvd光盘放到了一个网站项目中,然后不小心提交-a -m…而且,快,回购膨胀了2.2 g。下次我做了一些编辑,删除了视频文件,并提交了所有内容,但压缩文件仍然在存储库中,在历史中。
我知道我可以从这些提交中启动分支,并将一个分支重置到另一个分支上。但是我应该怎么做才能合并两次提交,使大文件不显示在历史记录中,并在垃圾收集过程中被清理?
当前回答
你可以使用branch filter命令:
git filter-branch -tree-filter 'rm -rf path/to/your/file' HEAD
其他回答
我基本上按照这个答案做了: https://stackoverflow.com/a/11032521/1286423
(对于历史,我复制粘贴在这里)
$ git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch YOURFILENAME" HEAD
$ rm -rf .git/refs/original/
$ git reflog expire --all
$ git gc --aggressive --prune
$ git push origin master --force
这并没有起作用,因为我喜欢重命名和移动东西。一些大文件在重命名的文件夹中,我认为gc不能删除对这些文件的引用因为树对象中的引用指向这些文件。 我最终的解决方法是:
# First, apply what's in the answer linked in the front
# and before doing the gc --prune --aggressive, do:
# Go back at the origin of the repository
git checkout -b newinit <sha1 of first commit>
# Create a parallel initial commit
git commit --amend
# go back on the master branch that has big file
# still referenced in history, even though
# we thought we removed them.
git checkout master
# rebase on the newinit created earlier. By reapply patches,
# it will really forget about the references to hidden big files.
git rebase newinit
# Do the previous part (checkout + rebase) for each branch
# still connected to the original initial commit,
# so we remove all the references.
# Remove the .git/logs folder, also containing references
# to commits that could make git gc not remove them.
rm -rf .git/logs/
# Then you can do a garbage collection,
# and the hidden files really will get gc'ed
git gc --prune --aggressive
我的repo (.git)从32MB变成了388KB,即使过滤器分支也无法清理。
如果你知道你的提交是最近的,而不是遍历整个树,执行以下操作: git filter-branch -tree filter 'rm LARGE_FILE.zip' HEAD~10.
除了git filter-branch(缓慢但纯粹的git解决方案)和BFG(更简单,性能非常好)之外,还有另一个性能良好的过滤工具:
https://github.com/xoofx/git-rocket-filter
从它的描述来看:
git-rocket-filter的目的类似于git-filter-branch命令,但提供了以下独特的功能:
快速重写提交和树(从x10到x100的顺序)。 内置支持使用——keep(保存文件或目录)的白名单和使用——remove选项的黑名单。 使用.gitignore类似的模式进行树过滤 快速和简单的c#脚本提交过滤和树过滤 支持每个文件/目录模式的树过滤脚本 自动修剪空的/不变的提交,包括合并提交
我用一个bitbucket帐户遇到了这个问题,我不小心在那里存储了我网站的巨大*.jpa备份。
git filter-branch——prune-empty——index-filter 'git rm -rf——cached——ignore-unmatch MY-BIG-DIRECTORY-OR-FILE'——tag-name-filter cat -- --all
用所讨论的文件夹重新安装MY-BIG-DIRECTORY,以完全重写历史记录(包括标记)。
来源:https://web.archive.org/web/20170727144429/http: / / naleid.com: 80 /博客/ 2012/01/17 / finding-and-purging-big-files-from-git-history /
为什么不使用这个简单而强大的命令呢?
git filter-branch --tree-filter 'rm -f DVD-rip' HEAD
——tree-filter选项在项目每次签出后运行指定的命令,然后重新提交结果。在这种情况下,您从每个快照中删除一个名为DVD-rip的文件,无论它是否存在。
如果你知道是哪个提交引入了这个巨大的文件(比如35dsa2),你可以用35dsa2替换HEAD。HEAD以避免重写太多的历史,从而避免在还没有推送的情况下出现不同的提交。@alpha_989提供的这个评论似乎太重要了,不能在这里省略。
请看这个链接。