清理.git中的无用文件

首先看下清理之前的目录大小

# bowang @ localhost in ~/mytest/scmtest on git:master o [10:05:08]
$ du -ah
······
 20K    ./.git/objects/pack/pack-3f4446b1e86d2111d80dd9ffd0ca30c3669f9145.idx
 18M    ./.git/objects/pack/pack-3f4446b1e86d2111d80dd9ffd0ca30c3669f9145.pack
 18M    ./.git/objects/pack
 18M    ./.git/objects
······
 18M    .

以下操作需谨慎,非常危险!别到时候从删库到跑路了

找出git中前几个大文件

git verify-pack -v .git/objects/pack/pack-*.idx | sort -k 3 -g | tail -5
# bowang @ localhost in ~/mytest/scmtest on git:master o [10:09:03]
$ git verify-pack -v .git/objects/pack/pack-*.idx | sort -k 3 -g | tail -5
d012feaf2caa9c236d1cd9c0936384296918288f blob   598978 520889 3158357
14104ce1fdce0275b2a11892691cb62c7882c8ed blob   762747 678885 1437035
0357e0fce32c35556b2ae2af88547b01c0be5082 blob   817936 724516 3906877
e05df3311f9c10e0fc975958746d013579f978a6 blob   1584904 720055 5426917
9069ef6ec87e24b3079333ff5de4c7217e195e6a blob   1971488 795297 4631620

第一行的字母相当于文件的id,用以下命令找出id对应的文件名

git rev-list --objects --all | grep <大文件id>
# bowang @ localhost in ~/mytest/scmtest on git:master o [10:12:04]
$ git rev-list --objects --all | grep d012feaf2caa9c236d1cd9c0936384296918288f
d012feaf2caa9c236d1cd9c0936384296918288f target/scmtest/WEB-INF/lib/spring-test-4.3.7.RELEASE.jar

# bowang @ localhost in ~/mytest/scmtest on git:master o [10:13:52]
$ git rev-list --objects --all | grep 14104ce1fdce0275b2a11892691cb62c7882c8ed
14104ce1fdce0275b2a11892691cb62c7882c8ed target/scmtest/WEB-INF/lib/spring-beans-4.3.7.RELEASE.jar

# bowang @ localhost in ~/mytest/scmtest on git:master o [10:13:59]
$ git rev-list --objects --all | grep 0357e0fce32c35556b2ae2af88547b01c0be5082
0357e0fce32c35556b2ae2af88547b01c0be5082 target/scmtest/WEB-INF/lib/spring-web-4.3.7.RELEASE.jar

最重要的两条命令是 git filter-branch 和 gc, filter-branch 真正在清理,但是只运行它也是没用的,需要再删除备份的文件,重新打包之类的,最后的gc命令,用来收集产生的垃圾,最终清除大文件

git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch <无用的大文件>' \
  --prune-empty --tag-name-filter cat -- --all
# bowang @ localhost in ~/mytest/scmtest on git:master o [10:14:18]
$ git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch target/scmtest/WEB-INF/lib/spring-test-4.3.7.RELEASE.jar' \
  --prune-empty --tag-name-filter cat -- --all
Rewrite 3d89aac1ab30081b4337250b764f853763241e67 (150/189) (6 seconds passed, remaining 1 predicted)
······

清理大文件

git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
# bowang @ localhost in ~/mytest/scmtest on git:master o [10:16:45]
$ git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin

# bowang @ localhost in ~/mytest/scmtest on git:master o [10:23:24]
$ git reflog expire --expire=now --all

# bowang @ localhost in ~/mytest/scmtest on git:master o [10:23:29]
$ git gc --prune=now
Counting objects: 656, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (301/301), done.
Writing objects: 100% (656/656), done.
Total 656 (delta 224), reused 611 (delta 220)

查看目录大小 a表示显示目录下所有的文件和文件夹(不含子目录),h表示以人类能看懂的方式

du -ah
# bowang @ localhost in ~/mytest/scmtest on git:master o [10:23:38]
$ du -ah
······
 20K    ./.git/objects/pack/pack-7ecdea2bc0c50abbe05898f9bf8c7db56bed701a.idx
5.5M    ./.git/objects/pack/pack-7ecdea2bc0c50abbe05898f9bf8c7db56bed701a.pack
5.6M    ./.git/objects/pack
5.6M    ./.git/objects
······
5.8M    .
改动提交到远端仓库
git push --force --verbose --dry-run
git push --force

你可能感兴趣的:(git)