简体   繁体   English

减少 git 存储库大小

[英]Reduce git repository size

I tried looking for a good tutorial on reducing repo size, but found none.我试图寻找一个关于减少 repo 大小的好教程,但没有找到。 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 大小...大约 10 MB,但问题是 Heroku 只允许 50 MB,而且我还没有完成我的应用程序的开发。

I added the usual suspects (log, vendor, doc etc) to .gitignore already.我已经将常见的嫌疑人(日志、供应商、文档等)添加到 .gitignore 中。 Although I only added .gitignore recently.虽然我最近才加了 .gitignore 。

Any suggestions?有什么建议么?

Update Feb. 2021, eleven years later: the new git maintenance command ( man page ) should supersede git gc , and can be scheduled .十一年后的 2021 年 2 月更新:新的git maintenance命令手册页)应该取代git gc ,并且可以安排


Original: git gc --aggressive is one way to force the prune process to take place (to be sure: git gc --aggressive --prune=now ).原文: git gc --aggressive是强制进行修剪过程的一种方法(可以肯定的是: git gc --aggressive --prune=now )。 You have other commands to clean the repo too.你也有其他命令来清理 repo。 Don't forget though, sometimes git gc alone can increase the size of the repo !不过不要忘记,有时单独git gc可以增加 repo 的大小

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);它也可以在filter-branch之后使用,以标记一些要从历史记录中删除的目录(进一步增加空间); see here .这里 But that means nobody is pulling from your public repo.但这意味着没有人会从您的公共回购中撤出。 filter-branch can keep backup refs in .git/refs/original , so that directory can be cleaned too. filter-branch可以将备份 refs 保存在.git/refs/original中,因此也可以清理该目录。

Finally, as mentioned in this comment and 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 存储库中删除未使用的对象

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 多一点。

In my case, I pushed several big (> 100Mb) files and then proceeded to remove them.就我而言,我推送了几个大(> 100Mb)文件,然后继续删除它们。 But they were still in the history of my repo, so I had to remove them from it as well.但是它们仍然在我的回购的历史中,所以我也不得不将它们从中删除。

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

Then, you need to push force on your branch:然后,您需要对分支施加压力:

git push origin <your_branch_name> --force

Note : bfg is a tool that can be installed on Linux and macOS using brew:注意bfg是一个可以使用 brew 安装在 Linux 和 macOS 上的工具:

brew install bfg

This should not affect everyone, but one of the semi-hidden reasons of the repository size being large could be Git submodules.这不应该影响每个人,但是存储库大小较大的半隐藏原因之一可能是 Git 子模块。

You might have added one or more submodules, but stopped using it at some time, and some files remained in .git/modules directory.您可能已经添加了一个或多个子模块,但在某个时候停止使用它,并且一些文件保留在.git/modules目录中。 To give redundant submodule files away, see this question .要放弃冗余子模块文件,请参阅此问题

However, just like the main repository, the other way is to navigate to the submodule directory in .git/modules , and do, for example, git gc --aggressive --prune .但是,就像主存储库一样,另一种方法是导航到.git/modules中的子模块目录,然后执行例如git gc --aggressive --prune

These should have a good impact on the repository size, but as long as you use Git submodules, eg especially with large libraries, your repository size should not change drastically.这些应该对存储库大小有很好的影响,但是只要您使用 Git 子模块,例如特别是对于大型库,您的存储库大小就不会发生剧烈变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM