清理git仓库记录.git/objects/pack内的大文件

最近发现构建任务每次拉取源码都要十几分钟,还以为是网络问题,看了一下带宽10M/s的速度,就把代码clone下来查看了一番,结果发现是.git文件太大了

看到源码里的pack-de142cec88f171d669655695a176e11d052ad2fa.pack达到惊人的5.3G,既然发现了那就准备开始清理

首先查找最占空间的大文件名

# git rev-list –objects –all | grep “$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -20 | awk ‘{print$1}’)”

rev-list:用来列出Git仓库中的提交,列出所有提交中涉及的文件名及其ID。

–objects:列出该提交涉及的所有文件ID。

–all:所有分支的提交,相当于指定了位于/refs下的所有引用。

verify-pack:命令用于显示已打包的内容。

果不其然大文件都sql文件

开始删除操作

# git filter-branch –force –index-filter ‘git rm -rf –cached –ignore-unmatch xxx.sql’ –prune-empty –tag-name-filter cat — –all

filter-branch:可以用来重写Git仓库中的提交

–index-filter:用来指定一条Bash命令,然后Git会检出(checkout)所有的提交, 执行该命令,然后重新提交。

–all:表示我们需要重写所有分支(或引用)。

注意上述我列出了20个最大的文件,那么这里只删除一个xxx.sql是不全的,可以把这些文件名写入一个txt文档,然后通过cat挨个进行删除

推荐先这种方法,简单省事儿有效

# cat sql.txt

xxx.sql xxx.sql xxx.sql xxx.sql xxx.sql xxx.sql xxx.sql xxx.sql xxx.sql xxx.sql

# git filter-branch -f –prune-empty –index-filter “git rm -rf –cached –ignore-unmatch `cat sql.txt`” –tag-name-filter cat — –all

删除之后,强制覆盖的形式推送上去,我这里选择的分支是develop分支(不敢在主分支上霍霍怕挨揍)

# git push origin develop –force # 只推送到develop分支

# git push origin –force –all # 推送全部分支

三点特别注意:

第一:如果push的时候提示保护分支不可写,那么就在gitlab里设置去掉该分支为保护分支

第二:删除大文件需要删除全部分支的大文件,如果某个分支没有被删除大文件的话,那么这个分支在提交的时候会把这些记录再度写回去

第三:如果其他分支的人不想执行这么繁琐的删除操作,那么等删除所有分支大文件之后,需要删除本地代码从新拉取本分支的代码,这样就能避免已删除的旧数据再度上传上去

然后进行GC操作,彻底清空不需要的垃圾

# rm -rf .git/refs/original/
# git reflog expire –expire=now –all
# git gc –prune=now

自此,清理空间完成

查看一下效果,在Jenkins里面,下面将近12分钟的co是删除之前的,上面1分半是删除之后的

11 评论

  1. Hello there, I found your website by means of Google at the same time as searching
    for a related matter, your website got here up, it looks great.
    I’ve bookmarked it in my google bookmarks.

    Hi there, simply changed into aware of your blog via Google, and located that it’s really informative.
    I’m going to be careful for brussels. I’ll be grateful when you continue this in future.
    A lot of folks will likely be benefited out of your writing.
    Cheers!

  2. Its like you read my mind! You appear to know a lot about this, like you wrote
    the book in it or something. I think that you can do with a
    few pics to drive the message home a little bit, but other than that, this is excellent blog.
    A fantastic read. I will definitely be back.

留下评论

error: Content is protected !!