我目前的基地总规模大约是。200 mb。
但是我的。git文件夹有惊人的5GB(!)。因为我把我的工作推到一个外部服务器,我不需要任何大的本地历史…
如何缩小。git文件夹以释放笔记本上的一些空间?我可以删除超过30天的所有更改吗?
我目前的基地总规模大约是。200 mb。
但是我的。git文件夹有惊人的5GB(!)。因为我把我的工作推到一个外部服务器,我不需要任何大的本地历史…
如何缩小。git文件夹以释放笔记本上的一些空间?我可以删除超过30天的所有更改吗?
当前回答
通过根据文件最近更新的时间从. Git文件夹中删除一些文件的日志历史来收缩Git存储库。
我在本地机器上也遇到过同样的问题。原因是我从本地删除了一些大型文件,并提交到中央存储库。但是事件发生在git状态之后,git fetch和git pull。我的。git文件夹大小大约是3GB。之后,我运行以下命令,通过考虑一个月前已经更改/过期的文件来减小.git文件夹的大小。
命令
$ git remote prune origin && git repack && git prune-packed && git reflog expire --expire=1.month.ago && git gc --aggressive
Git命令及其简短描述:
git-prune - Prune all unreachable objects from the object database git-repack - Pack unpacked objects in a repository git-prune-packed - Remove extra objects that are already in pack files. git reflog: Git keeps track of updates to the tip of branches using a mechanism called reference logs, or "reflogs." Reflogs track when Git refs were updated in the local repository. In addition to branch tip reflogs, a special reflog is maintained for the Git stash. Reflogs are stored in directories under the local repository's .git directory. git reflog directories can be found at .git/logs/refs/heads/., .git/logs/HEAD, and also .git/logs/refs/stash if the git stash has been used on the repo. git reflog at a high level on the Rewriting History Page. git reflog expire --expire=now --expire-unreachable=now --all In addition to preserving history in the reflog, Git has internal expiration dates on when it will prune detached commits. Again, these are all implementation details that git gc handles and git prune should not be used standalone. git gc --aggressive: git-gc - Cleanup unnecessary files and optimize the local repository.Behind the scenes git gc actually executes a bundle of other internal subcommands like git prune, git repack, git pack and git rerere. The high-level responsibility of these commands is to identify any Git objects that are outside the threshold levels set from the git gc configuration. Once identified, these objects are then compressed, or pruned accordingly.
常见的结果:
$ git remote prune origin && git repack && git prune-packed && git reflog expire --expire=1.month.ago && git gc --aggressive
Enumerating objects: 535, done.
Counting objects: 100% (340/340), done.
Delta compression using up to 2 threads
Compressing objects: 100% (263/263), done.
Writing objects: 100% (340/340), done.
Total 340 (delta 104), reused 0 (delta 0)
Enumerating objects: 904, done.
Counting objects: 100% (904/904), done.
Delta compression using up to 2 threads
Compressing objects: 100% (771/771), done.
Writing objects: 100% (904/904), done.
Total 904 (delta 343), reused 561 (delta 0)
其他回答
以下是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.
来源:http://gcc.gnu.org/ml/gcc/2007-12/msg00165.html
这将摆脱二进制数据是孤儿在我的回购?“git重新打包”将不会删除你已经签入repo然后删除它的图像或二进制数据。要从你的回购中永久删除这些数据,你必须重写你的历史记录。一个常见的例子是当你不小心在git中检入你的密码。你可以回去删除一些文件,但你必须重写从那时到现在的历史记录,然后强制将新的repo推到你的原点。
我更多地将git用作同步机制,而不是版本历史。所以我对这个问题的解决方案是确保我所有的当前源都处于令人满意的状态,然后删除.git并重新初始化repo。磁盘空间问题解决。:-)历史消失了:-( 我这样做是因为我的回购是在一个小的u盘上。我不想要也不需要我的全部历史。 如果我有一种截断历史的方法,我就会使用它。
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).
5GB vs 200MB有点奇怪。尝试运行git gc。
但是,除非您将存储库拆分为模块,否则您无法减小.git目录的大小。
git repo的每个克隆都是一个完整的存储库,可以充当服务器。这是分布式版本控制的基本原理。
通过根据文件最近更新的时间从. Git文件夹中删除一些文件的日志历史来收缩Git存储库。
我在本地机器上也遇到过同样的问题。原因是我从本地删除了一些大型文件,并提交到中央存储库。但是事件发生在git状态之后,git fetch和git pull。我的。git文件夹大小大约是3GB。之后,我运行以下命令,通过考虑一个月前已经更改/过期的文件来减小.git文件夹的大小。
命令
$ git remote prune origin && git repack && git prune-packed && git reflog expire --expire=1.month.ago && git gc --aggressive
Git命令及其简短描述:
git-prune - Prune all unreachable objects from the object database git-repack - Pack unpacked objects in a repository git-prune-packed - Remove extra objects that are already in pack files. git reflog: Git keeps track of updates to the tip of branches using a mechanism called reference logs, or "reflogs." Reflogs track when Git refs were updated in the local repository. In addition to branch tip reflogs, a special reflog is maintained for the Git stash. Reflogs are stored in directories under the local repository's .git directory. git reflog directories can be found at .git/logs/refs/heads/., .git/logs/HEAD, and also .git/logs/refs/stash if the git stash has been used on the repo. git reflog at a high level on the Rewriting History Page. git reflog expire --expire=now --expire-unreachable=now --all In addition to preserving history in the reflog, Git has internal expiration dates on when it will prune detached commits. Again, these are all implementation details that git gc handles and git prune should not be used standalone. git gc --aggressive: git-gc - Cleanup unnecessary files and optimize the local repository.Behind the scenes git gc actually executes a bundle of other internal subcommands like git prune, git repack, git pack and git rerere. The high-level responsibility of these commands is to identify any Git objects that are outside the threshold levels set from the git gc configuration. Once identified, these objects are then compressed, or pruned accordingly.
常见的结果:
$ git remote prune origin && git repack && git prune-packed && git reflog expire --expire=1.month.ago && git gc --aggressive
Enumerating objects: 535, done.
Counting objects: 100% (340/340), done.
Delta compression using up to 2 threads
Compressing objects: 100% (263/263), done.
Writing objects: 100% (340/340), done.
Total 340 (delta 104), reused 0 (delta 0)
Enumerating objects: 904, done.
Counting objects: 100% (904/904), done.
Delta compression using up to 2 threads
Compressing objects: 100% (771/771), done.
Writing objects: 100% (904/904), done.
Total 904 (delta 343), reused 561 (delta 0)
最好的选择是使用BFG Repo Cleaner (BitBucket推荐使用,其他选项要快得多): https://rtyley.github.io/bfg-repo-cleaner/
我也尝试过使用史蒂夫·洛雷克的解决方案,它也有效:https://web.archive.org/web/20190207210108/http://stevelorek.com/how-to-shrink-a-git-repository.html