让 Git 消耗更少的磁盘空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1398919/
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
Make Git consume less disk space?
提问by Joao Trindade
What is the best way for git to consume less disk space?
git 消耗更少磁盘空间的最佳方法是什么?
I'm using git-gcon my repositories (which does help, especially if there have been many commits since it was cloned) but I would like suggestions if there is any other command to shrink the disk space used by git.
我在我的存储库上使用git-gc(这确实有帮助,特别是如果自从它被克隆以来已经有很多提交)但是如果有任何其他命令来缩小 git 使用的磁盘空间,我想得到建议。
采纳答案by Tilka
git-gc calls lots of other commands that are used to clean up and compress the repository. All you could do is delete some old unused branches.
git-gc 调用了许多用于清理和压缩存储库的其他命令。您所能做的就是删除一些旧的未使用的分支。
Short answer: No :-(
简短回答:不:-(
回答by Jakub Nar?bski
There are a few suggestions I can offer:
我可以提供一些建议:
Delete no longer used branches. They can pin some commits that you don't use and would never use. Take care however to not delete branches that you would later need (perhaps for review, or for comparison of failed effort). Backup first.
Check if you didn't commit some large binary file (perhaps some generated file) by mistake. If you have, you can purge it from history using "git filter-branch"... well, if you didn't share the repository, or it is worth aggravating other contributors to rewrite history. Again: backup first.
You can prune more aggressively, discarding some safeties, bu using
git gc --prune=now
, or low-levelgit prune
. But take care that you don't remove safeties and backups (like reflog) that you need minute after compacting.Perhaps what enlarges your repository are some untracked files in working directory. There "make clean" or "git clean" might help (but take care that you don't remove some important files).
Most safe of all those suggestions: you can try to pack more aggressively, using
--depth
and--window
option of low-levelgit-repack
. See also Git Repack Parametersblog post by Pieter de Bie on his DVCS Comparison blog, from June 6, 2008. Or "git gc --aggressive
".
删除不再使用的分支。他们可以固定一些您不使用并且永远不会使用的提交。但是请注意不要删除您以后需要的分支(也许是为了,或者是为了比较失败的努力)。先备份。
检查您是否没有错误地提交一些大的二进制文件(可能是一些生成的文件)。如果有,您可以使用“git filter-branch”将其从历史记录中清除......好吧,如果您没有共享存储库,或者值得激怒其他贡献者重写历史记录。再次:先备份。
您可以更积极地修剪,丢弃一些安全性、使用
git gc --prune=now
或低级git prune
. 但是请注意不要在压缩后一分钟删除安全和备份(如引用日志)。也许扩大您的存储库的是工作目录中的一些未跟踪文件。“make clean”或“git clean”可能会有所帮助(但请注意不要删除一些重要文件)。
所有这些建议中最安全的:您可以尝试更积极地打包,使用
--depth
和--window
选择低级别git-repack
. 另请参阅Pieter de Bie 在其 DVCS 比较博客上发表的Git Repack Parameters博客文章,自 2008 年 6 月 6 日起。或“git gc --aggressive
”。
回答by adl
Depending on what you want to do with your repository, you might also consider using the following git clone
option:
根据您要对存储库执行的操作,您还可以考虑使用以下git clone
选项:
--depth <depth>
Create a shallow clone with a history truncated to the specified
number of revisions. A shallow repository has a number of
limitations (you cannot clone or fetch from it, nor push from nor
into it), but is adequate if you are only interested in the recent
history of a large project with a long history, and would want to
send in fixes as patches.
回答by curmil
Git clone now has a --single-branch option that allows you to checkout a single branch without pulling in the git history of the other branches. If git is consuming a lot of disk space because you have a lot of branches, you can delete your current checkout and re-clone the repo using this option to regain some disk space. For example:
Git clone 现在有一个 --single-branch 选项,允许您签出单个分支而无需拉入其他分支的 git 历史记录。如果 git 因为你有很多分支而消耗了大量磁盘空间,你可以删除当前的 checkout 并使用此选项重新克隆 repo 以重新获得一些磁盘空间。例如:
cd ../
rm -rf ./project
git clone -b master --single-branch [email protected]:username/project.git
Also, if your current master has a long history and you don't have any outstanding branches that need to be merged back into master, you can create an archive branch off of master and create a new orphan master with no git history:
此外,如果您当前的 master 有很长的历史,并且您没有任何需要合并回 master 的未完成分支,您可以从 master 创建一个存档分支,并创建一个没有 git 历史的新孤立 master:
git checkout -b master_archive_07162013 # create and switch to the archive branch
git push origin master_archive_07162013 # push the archive branch to the remote and track it
git branch -D master # delete local master
git push --delete origin master # delete remote master
git remote prune origin # delete the remote tracking branch
git checkout --orphan master # create a new master branch with no history
git commit -m "initial commit" # re-establish the files in the repo
git push origin master # push the new master to the remote
The new master branch's tree will not be related to the old archived master branch, so only do this when you are truly archiving the branch.
新的 master 分支的树将不会与旧的归档 master 分支相关,因此只有在您真正归档分支时才这样做。
If you archive your master branch and then git clone master with single-branch, your checkout should be a lot smaller.
如果你归档你的主分支,然后用单分支 git clone master,你的结帐应该小很多。
回答by Artelius
Every git repository contains the entire history. While git does a fairly good job of compressing this stuff, there's simply a lot of data in there.
每个 git 存储库都包含整个历史记录。虽然 git 在压缩这些东西方面做得相当不错,但里面只有很多数据。
The "obvious" but potentially not-possible-for-you solution is to start a new repository without all that old history.
“显而易见”但可能对您来说不可能的解决方案是在没有所有旧历史的情况下启动一个新存储库。
回答by Sardaukar
Git gc will remove unused objects. That is about everything you can do.
Git gc 将删除未使用的对象。这就是你能做的一切。
You could consider splitting up your repositories if they become too big.
如果存储库变得太大,您可以考虑拆分它们。
回答by Rory
You can repack your repository. However i think it's called by git gc
您可以重新打包存储库。但是我认为它是由git gc
git repack -ad
git repack -ad
回答by joeytwiddle
If you do not need to keep all of the commit history locally, you could use a shallow clone:
如果您不需要在本地保留所有提交历史,则可以使用浅克隆:
git clone --depth=1 [url_of_repo]
I frequently use this when cloning github projects, if I am only interested in the latest set of files and not in the history.
如果我只对最新的文件集而不对历史感兴趣,我在克隆 github 项目时经常使用它。
Apparently fetching and pushing is/was not support on shallow clones, but I have been able to successfully push and pull changes to github repos with it, so it might work in your case too. (But no doubt you will run into difficulties if you want to merge branches but don't have the base commit in history.)
显然,浅克隆不支持获取和推送,但我已经能够成功地使用它推送和拉取对 github 存储库的更改,因此它也可能适用于您的情况。(但毫无疑问,如果您想合并分支但没有历史记录中的基本提交,您会遇到困难。)
I think it is easier to start with a fresh clone as shown above, but others have shown how to trim an existing local repo.
我认为从上面显示的新克隆开始更容易,但其他人已经展示了如何修剪现有的本地 repo。
回答by knittl
git prune
might be a hint. it cleans the repository from unreachable commits (git gc
does not call it)
git prune
可能是一个提示。它从无法访问的提交中清除存储库(git gc
不调用它)
回答by CodingYourLife
Fool proof method if you don't care about download size is to delete the repository (you can just delete the whole folder) and add it again. Make sure everything that needs to be preserved is pushed to the server!
如果您不关心下载大小,则最简单的方法是删除存储库(您可以删除整个文件夹)并重新添加。确保需要保留的所有内容都推送到服务器!