如何缩小 .git 文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5613345/
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 10:35:18  来源:igfitidea点击:

How to shrink the .git folder

git

提问by JMW

My current base has a total size of approx. 200MB.

我目前的基地总规模约为。200MB。

But my .git folder has an amazing size of 5GB (!). Since I push my work to an external server, i don't need any big local history...

但是我的 .git 文件夹有惊人的 5GB (!) 大小。由于我将我的工作推送到外部服务器,因此我不需要任何大的本地历史记录...

How can I shrink the .git folder to free up some space on my notebook? Can I delete all changes that are older, than 30 days?

如何缩小 .git 文件夹以释放笔记本上的一些空间?我可以删除所有早于 30 天的更改吗?

thanks a lot for any help :)

非常感谢您的帮助:)

采纳答案by knittl

you should not delete all changes older than 30 days (i think it's somehow possible exploiting git, but really not recommended).

你不应该删除超过 30 天的所有更改(我认为它可能以某种方式利用 git,但真的不推荐)。

you can call git gc --aggressive --prune, which will perform garbage collection in your repository and prune old objects. do you have a lot of binary files (archives, images, executables) which change often? those usually lead to huge .git folders (remember, git stores snapshots for each revision and binary files compress badly)

您可以调用git gc --aggressive --prune,它将在您的存储库中执行垃圾收集并修剪旧对象。您是否有很多经常更改的二进制文件(档案、图像、可执行文件)?这些通常会导致巨大的 .git 文件夹(记住,git 存储每个修订版的快照,二进制文件压缩得很厉害)

回答by David Dehghan

Here is what the creator of git Linushas to say about how to shrink your git repo:

以下是 git Linus的创建者关于如何缩小您的 git 存储库的评论:

The equivalent of "git gc --aggressive" - but done *properly* - is to do (overnight) something like

   git repack -a -d --depth=250 --window=250

where that depth thing is just about how deep the delta chains can be (make them longer for old history - it's worth the space overhead), and the window thing is about how big an object window we want each delta candidate to scan.

And here, you might well want to add the "-f" flag (which is the "drop all old deltas", since you now are actually trying to make sure that this one actually finds good candidates.

“git gc --aggressive”的等价物 - 但完成*正确* - 是做(一夜之间)类似的事情

   git repack -a -d --depth=250 --window=250

深度是关于 delta 链的深度(让它们在旧历史中更长 - 空间开销是值得的),而窗口则是关于我们希望每个 delta 候选者扫描的对象窗口有多大。

在这里,您可能想添加“-f”标志(即“删除所有旧增量”,因为您现在实际上是在尝试确保该标志确实找到了好的候选者。

source: http://gcc.gnu.org/ml/gcc/2007-12/msg00165.html

来源:http: //gcc.gnu.org/ml/gcc/2007-12/msg00165.html

Will this get rid of binary data that is orphaned in my repo?"git repack" will not git rid of images or binary data that you have checked into your repo and then deleted it. To delete those kind of data permanently from your repo you have to re-write your history. A common example of that is when you accidentally check in your passwords in git. You can go back and delete some files but then you have to re-write your history from then to now and then force push then new repo to your origin.

这会摆脱我的回购中孤立的二进制数据吗?“git repack”不会删除您已签入存储库然后将其删除的图像或二进制数据。要从您的存储库中永久删除此类数据,您必须重新编写历史记录。一个常见的例子是当您不小心在 git 中签入密码时。您可以返回并删除一些文件,但是您必须从那时到现在重新编写您的历史记录,然后强制将新的存储库推送到您的源。

回答by Chris Hinshaw

I tried these but my repository was still very large. The problem was I had accidentally checked in some generated large files. After some searching I found a great tutorial which makes it easy to delete the large generated files. This tutorial allowed me to shrink my repository from 60 MB to < 1 MB.

我尝试了这些,但我的存储库仍然非常大。问题是我不小心检查了一些生成的大文件。经过一番搜索,我找到了一个很棒的教程,可以轻松删除生成的大文件。本教程允许我将存储库从 60 MB 缩小到 < 1 MB。

Steve Lorek, How to Shrink a Git Repository

Steve Lorek,如何缩小 Git 存储库

回答by ?imon Tóth

5GB vs 200MB is kind of weird. Try to run git gc.

5GB vs 200MB 有点奇怪。尝试运行git gc

But no, unless you split your repository into modules, you can't decrease the size of the .gitdirectory.

但是不行,除非您将存储库拆分为模块,否则无法减小.git目录的大小。

Each clone of a git repo is a full fledged repository that can act as a server. That's the base principle of distributed version control.

git repo 的每个克隆都是一个完整的存储库,可以充当服务器。这就是分布式版本控制的基本原则。

回答by Darrel Lee

I'm using git more as synchronization mechanism than for version history. So my solution to this problem has been to make sure I have all my current sources in a satisfactory state, and then just delete .git and re-initialize the repos. Disk space problem solved. :-) History gone :-( I do this because my repo is on a small USB key. I don't want or need my entire history. If I had a method for just truncating the history, I would use that.

我更多地使用 git 作为同步机制而不是版本历史。所以我对这个问题的解决方案是确保我所有当前的源都处于令人满意的状态,然后删除 .git 并重新初始化存储库。磁盘空间问题解决。:-) 历史消失了 :-( 我这样做是因为我的回购在一个小的 USB 钥匙上。我不想要或不需要我的整个历史。如果我有一种方法可以截断历史,我会使用它。

If I were interested in keeping my history I would archive the current repository. At some point later I could clone the original repository, copy over all the changes from the new repo (let's assume I haven't done much (any) renaming or deleteing). And then make one big commit that would represent all the changes made in the new repo as a single commit in the old repo. Is it possible to merge the histories? Maybe if I used a branch and then deleted the objects I didn't need. (I dont' know enough about git internals to start fooling around like that).

如果我有兴趣保留我的历史记录,我会将当前存储库存档。稍后我可以克隆原始存储库,复制新存储库中的所有更改(假设我没有做太多(任何)重命名或删除操作)。然后进行一次大提交,将新存储库中所做的所有更改表示为旧存储库中的单个提交。是否可以合并历史记录?也许如果我使用了一个分支然后删除了我不需要的对象。(我对 git 内部结构的了解不够,无法开始像这样鬼混)。

回答by JerryGoyal

Tried above methods, nothing worked in my case (where I accidently killed the git process during git push) so I finally had to delete the repo and clone it again and now the .git folder is of normal size.

尝试了上述方法,在我的情况下没有任何效果(我在 git push 期间不小心杀死了 git 进程)所以我最终不得不删除 repo 并再次克隆它,现在 .git 文件夹大小正常。