让Git消耗更少的磁盘空间?

Joa*_*ade 40 git

git消耗更少磁盘空间的最佳方法是什么?

我在我的存储库上使用git-gc(这确实有帮助,特别是如果自克隆以来已经有很多提交)但我想建议是否有任何其他命令来缩小git使用的磁盘空间.

谢谢

Jak*_*ski 46

我可以提供一些建议:

  1. 删除不再使用的分支.他们可以固定一些您不使用但永远不会使用的提交.但请注意不要删除以后需要的分支(可能用于审核或比较失败的工作量).先备份.

  2. 检查您是否错误地提交了一些大型二进制文件(可能是某些生成的文件).如果你有,你可以使用"git filter-branch"从历史中清除它......好吧,如果你没有共享存储库,或者值得加重其他贡献者来重写历史记录.再次:先备份.

  3. 你可以更积极地修剪,丢弃一些安全,使用git gc --prune=now或低级别git prune.但请注意,您不要删除压缩后需要一分钟的安全和备份(如reflog).

  4. 也许扩大存储库的是工作目录中的一些未跟踪文件."make clean"或"git clean"可能有所帮助(但请注意不要删除一些重要文件).

  5. 所有这些建议中最安全的:你可以尝试更积极地使用--depth和使用--window低级别的选项git-repack.另见Pieter de Bie在2008年6月6日的DVCS比较博客上发表的Git Repack Parameters博客文章.或者" git gc --aggressive".


adl*_*adl 21

根据您对存储库的操作,您还可以考虑使用以下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.
Run Code Online (Sandbox Code Playgroud)

  • 看起来像git 1.9浅的克隆确实允许更多的互动.使用替换http://stackoverflow.com/a/17622991/25286在此SO-answer中描述了更好的解决方案 (2认同)

Til*_*lka 13

git-gc调用许多用于清理和压缩存储库的其他命令.你所能做的就是删除一些旧的未使用的分支.

简答:否:-(


cur*_*mil 6

Git clone现在有一个--single-branch选项,允许你签出一个分支而不需要拉入其他分支的git历史记录.如果git占用大量磁盘空间,因为你有很多分支,你可以删除当前的签出并使用此选项重新克隆repo以重新获得一些磁盘空间.例如:

cd ../
rm -rf ./project
git clone -b master --single-branch git@github.com:username/project.git
Run Code Online (Sandbox Code Playgroud)

此外,如果您当前的主服务器具有较长的历史记录且您没有任何需要合并回主服务器的未完成分支,则可以从主服务器创建存档分支并创建一个没有git历史记录的新的孤立主服务器:

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
Run Code Online (Sandbox Code Playgroud)

新的主分支树将不与旧的归档主分支相关联,因此只有在您真正归档分支时才这样做.

如果您将主分支存档,然后使用单分支存档git clone master,则结帐应该要小得多.


Art*_*ius 5

每个git存储库都包含整个历史记录.虽然git在压缩这些东西方面做得相当不错,但其中只有很多数据.

"明显的"但可能不可能的解决方案是在没有旧历史的情况下启动新的存储库.


joe*_*dle 5

如果您不需要在本地保留所有提交历史记录,则可以使用浅克隆:

git clone --depth=1 [url_of_repo]
Run Code Online (Sandbox Code Playgroud)

如果我只对最新的文件集而不是历史记录感兴趣,我在克隆 GitHub 项目时经常使用此功能。

显然,浅克隆不支持获取和推送,但我已经能够使用它成功地推送和拉取对 GitHub 存储库的更改,因此它也可能适用于您的情况。(但是毫无疑问,如果您想合并分支但历史上没有基础提交,您会遇到困难。)

我认为从上面所示的新克隆开始更容易,但其他人已经展示了如何修剪现有的本地存储库