【Git】Git瘦身,清理Git历史提交/.git大文件清理(云效、UI 自动化项目)

目前项目是存在云效(codeup.aliyun.com)上
本地清理后,还需要到云效上清理「存储空间管理」

      • 一、清理/瘦身效果
      • 二、到底是什么在占空间?
          • 1、先看一下项目里,什么最占空间?
          • 2、往下看在/.git里,什么最占空间?
      • 三、清理/瘦身过程
          • 1、git命令修改大文件提交记录
            • (1)查看历史提交中占用空间最多的10个文件
            • (2)重写提交记录,删除提交记录里面的大文件(删`data/apks/large.apk`)
            • (3)清理本地缓存+强推到远端
            • (4)本项目是存在云效codeup上,还需要到云效上手动清理下服务器缓存
            • (5)本地查看 & 远程查看效果
          • 2、BFG工具
            • (1)BFG下载到本地,改个名字-> bfg.jar
            • (2)镜像下载待瘦身项目
            • (3)用BFG清理文件
            • (4)本地清理+提交到远程

一、清理/瘦身效果


清理前:451.11M
【Git】Git瘦身,清理Git历史提交/.git大文件清理(云效、UI 自动化项目)_第1张图片

清理后:12.68M
【Git】Git瘦身,清理Git历史提交/.git大文件清理(云效、UI 自动化项目)_第2张图片

结论:清理效果喜人,清除了97%无用大文件提交




二、到底是什么在占空间?

在项目不断提交过程中,提交过一些大文件如:apk,ipa文件,
这类文件动不动3、40M,就算之后删掉这些大文件,.git提交里还是存着——

就这样在不断提交中,把整个项目扩张成一个非常大/占空间的项目。


1、先看一下项目里,什么最占空间?
~/project (xx ✔)du -d 1 -h 
524K    ./page
 48K    ./.pytest_cache
 16K    ./shell
4.0K    ./__pycache__
216K    ./common
2.2M    ./log
  0B    ./report
482M    ./.git
1.6M    ./data
684K    ./testcases
 40K    ./.idea
487M    .

首先确定项目里,最占空间的是.git

482M    ./.git
2、往下看在/.git里,什么最占空间?
/.git (master ✔)du -d 1 -h
448M	./objects
4.0K	./info
 12K	./logs
 60K	./hooks
8.0K	./refs
448M	.
# 448M	./objects 最大

/.git/objects (master ✔)du -d 1 -h
448M	./pack
  0B	./info
448M	.
# 448M	./pack 最大

.git/objects/pack (master ✔) ᐅ ll
total 916904
-r--r--r--  1 xxx  staff   287K Sep 15 17:31 pack-qqqqqqqqq.idx
-r--r--r--  1 xxx  staff   447M Sep 15 17:31 pack-wwwwwwwww.pack
# 447M Sep 15 17:31 pack-wwwwwwwww.pack 最大

破案,.git 里最占空间的是——/objects/pack
我们本次清理, 其实就是清理/objects/pack里的大文件。





三、清理/瘦身过程

⚠️⚠️⚠️
都直接在master分支操作,提前做好备份
⚠️⚠️⚠️

有2种方法清理:
(1)git命令修改大文件提交记录
(2)BFG工具


1、git命令修改大文件提交记录
(1)查看历史提交中占用空间最多的10个文件
project (master ✔)git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}')"

781f20bbd24b3c23eb7a706044ad6147cd9931b0 data/apks/large.apk
8c38ebb58c15adf81bb297db883e2c29eb1f64f5 data/apks/large.apk
cf7fe36048b822bc9c1784a6d68dd1ded177dd29 data/apks/large.apk
ff99a5b3486af7e47af3136d7630d18c3f8a0a14 data/apks/large.apk
878fe9c837664bc57082a011e16a779ec7562f4b data/apks/large.apk
ec9272fcbfc7e1dc3e318fe8c5805066a420b451 data/apks/large.apk
332bccff3731c2f67d8994908887bc9ae2131781 data/apks/large.apk
6aa7f12c992ec6a19372d13a6d1b47b0aef95e18 data/apks/large.apk
15525f8df6f2a04c5bb7b40b78a8959762c2f3db venv/lib/python3.9/site-packages/lxml/etree.cpython-39-darwin.so

提交最多的就是data/apks/large.apk

(2)重写提交记录,删除提交记录里面的大文件(删data/apks/large.apk
project (master ✔)git filter-branch --force --index-filter 'git rm -rf --cached --ignore-unmatch data/apks/large.apk' --prune-empty --tag-name-filter cat -- --all
WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...

Rewrite xxxccd26da5ecca698b61f85a0be378 (330/991) (41 seconds passed, remaining 82 predicted)    rm 'data/apks/large.apk'
Rewrite fae60972169db924e4e38ae2bbc5987a786224f7 (794/991) (98 seconds passed, remaining 24 predicted)    rm 'data/apks/large.apk'
Rewrite xxxc8cfe79b44e98d85f430b86c40af (803/991) (99 seconds passed, remaining 23 predicted)    rm 'data/apks/large.apk'
Rewrite xxx0394d9776a885567ffdb9368943 (983/991) (121 seconds passed, remaining 0 predicted)     
Ref 'refs/heads/master' was rewritten
Ref 'refs/heads/xxx' was rewritten
Ref 'refs/heads/qqq' was rewritten
Ref 'refs/heads/www' was rewritten
Ref 'refs/heads/eee' was rewritten
Ref 'refs/heads/rrr' was rewritten
Ref 'refs/heads/ttt' was rewritten
(3)清理本地缓存+强推到远端
rm -rf .git/refs/original/
git reflog expire --expire=now --all && git gc --prune=now --aggressive

git push --force
git remote prune origin  
(4)本项目是存在云效codeup上,还需要到云效上手动清理下服务器缓存

【Git】Git瘦身,清理Git历史提交/.git大文件清理(云效、UI 自动化项目)_第3张图片

(5)本地查看 & 远程查看效果

本地:

project (master ✔)du -d 1 -h
252K	./page
 16K	./shell
104K	./common
  0B	./log
  0B	./report
 13M	./.git
1.6M	./data
212K	./testcases
 15M	.

远程:
【Git】Git瘦身,清理Git历史提交/.git大文件清理(云效、UI 自动化项目)_第4张图片
清理完成✅



2、BFG工具
(1)BFG下载到本地,改个名字-> bfg.jar

BFG下载

(2)镜像下载待瘦身项目

$ git clone --mirror git://xxx.com/project.git

(3)用BFG清理文件

文件:large.apk

java -jar bfg.jar --delete-folders .git --delete-files large.apk  --no-blob-protection  project.git

Using repo : /Users/xxx/project.git

Found 0 objects to protect
Found 19 commit-pointing refs : HEAD, refs/heads/xxx, refs/heads/www, ...

Protected commits
-----------------

You're not protecting any commits, which means the BFG will modify the contents of even *current* commits.

This isn't recommended - ideally, if your current commits are dirty, you should fix up your working copy and commit that, check that your build still works, and only then run the BFG to clean up your history.

Cleaning
--------

Found 1298 commits
Cleaning commits:       100% (1298/1298)
Cleaning commits completed in 636 ms.
(4)本地清理+提交到远程
cd project.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive

git push --force

你可能感兴趣的:(git,自动化,经验分享,python,appium)