我不小心把一个dvd光盘放到了一个网站项目中,然后不小心提交-a -m…而且,快,回购膨胀了2.2 g。下次我做了一些编辑,删除了视频文件,并提交了所有内容,但压缩文件仍然在存储库中,在历史中。
我知道我可以从这些提交中启动分支,并将一个分支重置到另一个分支上。但是我应该怎么做才能合并两次提交,使大文件不显示在历史记录中,并在垃圾收集过程中被清理?
我不小心把一个dvd光盘放到了一个网站项目中,然后不小心提交-a -m…而且,快,回购膨胀了2.2 g。下次我做了一些编辑,删除了视频文件,并提交了所有内容,但压缩文件仍然在存储库中,在历史中。
我知道我可以从这些提交中启动分支,并将一个分支重置到另一个分支上。但是我应该怎么做才能合并两次提交,使大文件不显示在历史记录中,并在垃圾收集过程中被清理?
当前回答
除了git filter-branch(缓慢但纯粹的git解决方案)和BFG(更简单,性能非常好)之外,还有另一个性能良好的过滤工具:
https://github.com/xoofx/git-rocket-filter
从它的描述来看:
git-rocket-filter的目的类似于git-filter-branch命令,但提供了以下独特的功能:
快速重写提交和树(从x10到x100的顺序)。 内置支持使用——keep(保存文件或目录)的白名单和使用——remove选项的黑名单。 使用.gitignore类似的模式进行树过滤 快速和简单的c#脚本提交过滤和树过滤 支持每个文件/目录模式的树过滤脚本 自动修剪空的/不变的提交,包括合并提交
其他回答
你可以使用branch filter命令:
git filter-branch -tree-filter 'rm -rf path/to/your/file' HEAD
根据GitHub文档,只需遵循以下步骤:
去掉大文件
选项1:你不想保留大文件:
rm path/to/your/large/file # delete the large file
选项2:您希望将大文件保存到一个未跟踪的目录中
mkdir large_files # create directory large_files
touch .gitignore # create .gitignore file if needed
'/large_files/' >> .gitignore # untrack directory large_files
mv path/to/your/large/file large_files/ # move the large file into the untracked directory
保存更改
git add path/to/your/large/file # add the deletion to the index
git commit -m 'delete large file' # commit the deletion
从所有提交中删除大文件
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch path/to/your/large/file" \
--prune-empty --tag-name-filter cat -- --all
git push <remote> <branch>
如果你知道你的提交是最近的,而不是遍历整个树,执行以下操作: git filter-branch -tree filter 'rm LARGE_FILE.zip' HEAD~10.
在尝试了SO中的几乎所有答案后,我终于找到了这个gem,它可以快速删除我的存储库中的大文件,并允许我再次同步:http://www.zyxware.com/articles/4027/how-to-delete-files-permanently-from-your-local-and-remote-git-repositories
CD到本地工作文件夹,执行以下命令:
git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch FOLDERNAME" -- --all
将FOLDERNAME替换为你想从给定的git存储库中删除的文件或文件夹。
一旦完成,运行以下命令清理本地存储库:
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
现在将所有更改推到远程存储库:
git push --all --force
这将清理远程存储库。
git reset --soft HEAD~1
它将保留更改,但删除提交,然后您可以重新提交这些更改。