Git 清理

背景

git越来越大,每次git clone需要花很多时间,需要对项目做下瘦身,以cloudtools-xxxx

~/cloudtools-cmc ᐅ git count-objects -v -H
count: 0
size: 0 bytes
in-pack: 43428
packs: 1
size-pack: 1.32 GiB
prune-packable: 0
garbage: 0
size-garbage: 0 bytes

1.32 GiB 太大了,难以承受

备份测试库

  1. clone

    git clone --bare https://github.com/exampleuser/old-repository.git
  2. mirror

    $ cd old-repository.git
    $ git push --mirror https://github.com/exampleuser/new-repository.git
  3. clean

    $ cd ..
    $ rm -rf old-repository.git

清理无用的文件

  • 查询最大的三个文件
~/cloudtools-xxxx ᐅ git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -3
c92f94212ded1cd33da19d299fc0ffb107024655 blob   24003648 6450500 4141015
3b82e38c7adf1a6c4350cf59664d6cd798fc6b9f blob   28023079 27749442 55982962
15e2e2d4dea24a429929b0064b4983f6556736f1 blob   1266732157 1266807336 112364222
  • 查询文件到底是什么
~/cloudtools-xxxx ᐅ git rev-list --objects --all | grep 15e2e2d4dea24a429929b0064b4983f6556736f1
15e2e2d4dea24a429929b0064b4983f6556736f1 document.zip
~/cloudtools-xxxx ᐅ git rev-list --objects --all | grep 3b82e38c7adf1a6c4350cf59664d6cd798fc6b9f
3b82e38c7adf1a6c4350cf59664d6cd798fc6b9f dependencies/pypy-2.5.1-linux_x86_64-portable.tar.bz2
~/cloudtools-xxxx ᐅ git rev-list --objects --all | grep c92f94212ded1cd33da19d299fc0ffb107024655
c92f94212ded1cd33da19d299fc0ffb107024655 consul/consul
  • 删除这些已经不用的文件

拉取repo

 git clone --mirror ssh://git@bitbucket.xxxx.com:9999/cctgcloud/cloudtools-cmc.git

准备清理工具

 wget https://repo1.maven.org/maven2/com/madgag/bfg/1.13.0/bfg-1.13.0.jar

删除不需要的数据

java -jar bfg-1.13.0.jar --delete-folders dependencies cloudtools-xxxx.git --no-blob-protection
java -jar bfg-1.13.0.jar --delete-folders consul cloudtools-xxxx.git --no-blob-protection
java -jar bfg-1.13.0.jar --delete-files document.zip  cloudtools-xxxx.git --no-blob-protection

重新打包

cd cloudtools-xxxx.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
  • 重新push到仓库
git push

如果此时原库push不了,可以直接push至新库

git push --mirror git@sqbu-github.xxxx.com:cloudtools/xxxx.git

mumubin
375 声望185 粉丝