git 有没有办法减小git文件夹的大小?

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

Is there a way to reduce the size of the git folder?

git

提问by Sheehan Alam

Seems like my project is getting bigger and bigger with every git commit/push. Is there a way to clean up my git folder?

似乎我的项目随着每个 git 变得越来越大commit/push。有没有办法清理我的 git 文件夹?

回答by houbysoft

I'm not sure what you want. First of all, of course each time you commit/push the directory is going to get a little larger, since it has to store each of those additional commits.

我不确定你想要什么。首先,当然每次提交/推送目录都会变大一点,因为它必须存储每个额外的提交。

However, probably you want git gcwhich will "cleanup unnecessary files and optimize the local repository" (manual page).

但是,您可能想要git gc“清理不必要的文件并优化本地存储库”(手册页)。

Another possibly relevant command is git cleanwhich will deleteuntracked files from your tree (manual page).

另一种可能是相关的命令是git clean删除从你的树(未跟踪文件手册页)。

回答by phamductri

Run:

跑:

git remote prune origin

Deletes all stale tracking branches which have already been removed at originbut are still locally available in remotes/origin.

删除所有已被删除origin但在remotes/origin.

git gc --auto

'Garbage Collection' - runs housekeeping tasks (compresses revisions, removes loose/inaccessible objects). The --autoflag first determines whether any work is required, and exits without doing anything if not.

G ^arbage Çollection” -运行内务处理任务(压缩版本,排除松/不可访问的对象)。该--auto标志首先确定是否需要任何工作,如果不需要则退出而不做任何事情。

回答by VonC

One scenario where your git repo will get seriouslybigger with each commit is one where you are committing binary files that you generate regularly. Their storage won't be as efficient than text file.

其中一个场景,你的混帐回购协议将得到认真更大的每提交一个你在哪里犯的二进制文件,你经常产生。它们的存储效率不如 text file

Another is one where you have a huge number of files within onerepo (which is a limit of git) instead of several subrepos (managed as submodules).

另一种是在一个repo 中有大量文件(这是git限制)而不是几个 subrepos(作为 submodules 管理)。

In this article on git space, AlBlue mentions:

这篇关于 git space 的文章中,AlBlue 提到:

Note that Git (and Hg, and other DVCSs) do suffer from a problem where (large) binaries are checked in, then deleted, as they'll still show up in the repository and take up space, even if they're not current.

请注意,Git(以及 Hg 和其他 DVCS)确实存在签入(大型)二进制文件然后被删除的问题,因为它们仍然会出现在存储库中并占用空间,即使它们不是最新的.

If you have large binaries stored in your git repo, you may consider:

如果您的 git 存储库中存储了大型二进制文件,您可以考虑:

As I mentioned in "What are the file limits in Git (number and size)?", the more recent (2015, 5 years after this answer) Git LFSfrom GitHub is a way to manage those large files (by storing them outsidethe Git repository).

正如我提到“什么是Git中的文件限制(数量和大小)?”,最近的(2015年,这个答案后5年)的Git LFS从GitHub是管理这些大型文件(通过存储它们的方式以外的Git 存储库)。

回答by VonC

yes yes, git gcis the solution, naturally,

是的,是的,git gc是解决方案,自然,

and locally - you can just delete the local repository and clone it again,

和本地 - 您可以删除本地存储库并再次克隆它,

but there is something more important here...

但这里还有更重要的事情......

the seconds you wait for that huge git & externals to process are collected to long minutes in which are collected to hours of inefficient time spent,

你等待那个巨大的 git & externals 处理的秒数被收集到很长的分钟内,其中被收集到数小时的低效时间,

Create a new (entirely, not just a branch)repository from scratch, including the only recent version of files, naturallyyou'll loose all the history,

从头开始创建一个新的(完全,而不仅仅是一个分支)存储库,包括唯一最近版本的文件,自然会丢失所有历史记录,

but when in code-world it is not time to get sentimental, there is no point dragging along the entire 5 years of code every commit or diff, you can still store the old git & externals somewhere, if you get nostalgic :]

但是当在代码世界中没有时间感伤时,每次提交或差异都没有必要拖延整个 5 年的代码,如果您怀旧,您仍然可以将旧的 git & externals 存储在某处:]

but, at some point you really have to move along :]

但是,在某些时候,您真的必须继续前进:]

your team will thank you!

你的团队会感谢你!

回答by user

Running this command is extremely dangerous, but will shrink your repository by erasing all your git recovery/backup files:

运行此命令极其危险,但会通过擦除所有 git 恢复/备份文件来缩小存储库:

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

It will erase all files git uses to recover your repository from some bad command, for example, if you did git reset --hard, you can usually recover the files lost. But if you do git reset --hardbefore the git reflog expire...command, then you lost everything. Now, your only hope is to use some tool which analyses your file system and try to recover the erased files, if they were not overridden.

它会删除 git 用来从一些错误命令中恢复存储库的所有文件,例如,如果你这样做了git reset --hard,你通常可以恢复丢失的文件。但是如果你git reset --hardgit reflog expire...命令之前做,那么你就失去了一切。现在,您唯一的希望是使用一些工具来分析您的文件系统并尝试恢复已删除的文件(如果它们没有被覆盖)。

回答by anandharshan

git clean -d -f -iis the best way to do it.

git clean -d -f -i是最好的方法。

This will help to clean in a more controlled manner.

这将有助于以更可控的方式进行清洁。

-istands for interactive.

-i代表互动。

回答by Damien Sawyer

Don't know if it will shrink it, but after I run git clean, I often do git repack -adas well, which reduces the number of pack files.

不知道会不会缩小,但是我跑了之后git clean,也经常这样git repack -ad,减少了pack文件的数量。