减少 git 存储库大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2116778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:42:04  来源:igfitidea点击:

Reduce git repository size

gitgit-clean

提问by sent-hil

I tried looking for a good tutorial on reducing repo size, but found none. How do I reduce my repo size...it's about 10 MB, but the thing is Heroku only allows 50 MB and I'm no where near finished developing my app.

我试图寻找一个关于减少 repo 大小的好教程,但没有找到。我如何减少我的 repo 大小...大约 10 MB,但问题是 Heroku 只允许 50 MB,而且我还没有完成我的应用程序的开发。

I added the usual suspects (log, vendor, doc etc) to .gitignore already. Although I only added .gitignore recently.

我已经向 .gitignore 添加了通常的嫌疑人(日志、供应商、文档等)。虽然我最近才添加了 .gitignore。

Any suggestions?

有什么建议?

回答by VonC

git gc --aggressiveis one way to force the prune process to take place (to be sure: git gc --aggressive --prune=now). You have other commandsto clean the repo too. Don't forget though, sometimes git gcalone can increase the size of the repo!

git gc --aggressive是强制修剪过程发生的一种方法(可以肯定:)git gc --aggressive --prune=now。您还有其他命令来清理存储库。但是不要忘记,有时git gc单独可以增加回购的大小

It can be also used after a filter-branch, to mark some directories to be removed from the history (with a further gain of space); see here. But that means nobody is pulling from your public repo. filter-branchcan keep backup refs in .git/refs/original, so that directory can be cleaned too.

也可以在filter-branch,之后使用,以标记要从历史记录中删除的某些目录(进一步增加空间);看到 这里。但这意味着没有人从您的公共回购中提取。filter-branch可以将备份引用保留在 中.git/refs/original,以便也可以清理该目录。

Finally, as mentioned in this commentand this question; cleaning the reflog can help:

最后,如本评论本问题所述;清理 reflog 可以帮助:

git reflog expire --all --expire=now
git gc --prune=now --aggressive

An even more complete, and possibly dangerous, solution is to remove unused objects from a git repository

一个更完整但可能更危险的解决方案是从 git 存储库中删除未使用的对象

回答by sent-hil

Thanks for your replies. Here's what I did:

感谢您的回复。这是我所做的:

git gc
git gc --aggressive
git prune

That seemed to have done the trick. I started with around 10.5MB and now it's little more than 980KBs.

这似乎成功了。我从大约 10.5MB 开始,现在它略高于 980KB。

回答by vinzee

In my case, I pushed several big (> 100Mb) files and then proceeded to remove them. But they were still in the history of my repo, so I had to remove them from it as well.

就我而言,我推送了几个大(> 100Mb)文件,然后继续删除它们。但是它们仍然在我的回购历史中,所以我也不得不将它们从其中删除。

What did the trick was:

诀窍是什么:

bfg -b 100M  # To remove all blobs from history, whose size is superior to 100Mb
git reflog expire --expire=now --all
git gc --prune=now --aggressive

bfgis a tool that can be installed on Linux and macOS using brew:

bfg是一个可以使用 brew 安装在 Linux 和 macOS 上的工具:

brew install bfg