如何从 git 存储库中删除未使用的对象? [英] How to remove unused objects from a git repository?

查看:55
本文介绍了如何从 git 存储库中删除未使用的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不小心在 Git 存储库中添加、提交并推送了一个巨大的二进制文件,其中包含我最新的提交.

I accidentally added, committed and pushed a huge binary file with my very latest commit to a Git repository.

如何让 Git 删除为该提交创建的对象,以便我的 .git 目录再次缩小到正常大小?

How can I make Git remove the object(s) that was/were created for that commit so my .git directory shrinks to a sane size again?

编辑:感谢您的回答;我尝试了几种解决方案.没有一个工作.例如 GitHub 上的一个从历史记录中删除了文件,但 .git 目录大小没有减少:

Edit: Thanks for your answers; I tried several solutions. None worked. For example the one from GitHub removed the files from the history, but the .git directory size hasn't decreased:

$ BADFILES=$(find test_data -type f -exec echo -n "'{}' " ;)

$ git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch $BADFILES" HEAD
Rewrite 14ed3f41474f0a2f624a440e5a106c2768edb67b (66/66)
rm 'test_data/images/001.jpg'
[...snip...]
rm 'test_data/images/281.jpg'
Ref 'refs/heads/master' was rewritten

$ git log -p # looks nice

$ rm -rf .git/refs/original/
$ git reflog expire --all
$ git gc --aggressive --prune
Counting objects: 625, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (598/598), done.
Writing objects: 100% (625/625), done.
Total 625 (delta 351), reused 0 (delta 0)

$ du -hs .git
174M    .git
$ # still 175 MB :-(

推荐答案

我在别处回答了这个问题,我会复制到这里,因为我为此感到自豪!

I answered this elsewhere, and will copy here since I'm proud of it!

... 不用多说,我可以向你展示这个有用的脚本,git-gc-all,保证删除所有的 git 垃圾,直到他们可能提出额外的配置变量:

... and without further ado, may I present to you this useful script, git-gc-all, guaranteed to remove all your git garbage until they might come up with extra config variables:

git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 
  -c gc.rerereresolved=0 -c gc.rerereunresolved=0 
  -c gc.pruneExpire=now gc "$@"

--aggressive 选项可能会有所帮助.

The --aggressive option might be helpful.

注意:这将删除所有未引用的东西,所以如果您以后决定要保留其中的一些,请不要来找我!

NOTE: this will remove ALL unreferenced thingies, so don't come crying to me if you decide later that you wanted to keep some of them!

你可能还需要先运行这些东西,哦,天哪,git 很复杂!!

You might also need to run something like these first, oh dear, git is complicated!!

git remote rm origin
rm -rf .git/refs/original/ .git/refs/remotes/ .git/*_HEAD .git/logs/
git for-each-ref --format="%(refname)" refs/original/ |
  xargs -n1 --no-run-if-empty git update-ref -d

我把所有这些都放在一个脚本中,在这里:

I put all this in a script, here:

http://sam.nipl.net/b/git-gc-万能的

这篇关于如何从 git 存储库中删除未使用的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆