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

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

问题描述



我如何使Git删除对象,那是为那个提交创建的,所以我的 .git 目录再次缩小到一个合理的大小?



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

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

$ git filter-branch - -index-filtergit rm -rf --cached --ignore-unmatch $ BADFILESHEAD
重写14ed3f41474f0a2f624a440e5a106c2768edb67b(66/66)
rm'test_data / images / 001.jpg'
[... snip ...]
rm'test_data / images / 281.jpg'
Ref'refs / heads / master'被重写

$ git log -p #看起来不错

$ rm -rf .git / refs / original /
$ git reflog expire --all
$ git gc --aggressive --prune
计数对象:625,完成。
使用最多2个线程的Delta压缩。
压缩对象:100%(598/598),完成。
写入对象:100%(625/625),完成。
总计625(delta 351),重用0(delta 0)

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


解决方案

我在其他地方回答,为此感到自豪!



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

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

--aggressive选项可能会有帮助。



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



您可能还需要运行某些东西喜欢这些,哦,亲爱的,git是com p $

  git remote rm origin 
rm -rf .git / refs /原始/ .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

我把这一切放在一个脚本中,在这里:



http://sam.nipl.net/b/git-gc-all-ferocious


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

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?

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!

... 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 "$@"

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!

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-all-ferocious

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

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