4

Git 仓库瘦身

 2 years ago
source link: https://exp-blog.com/scm/git-cang-ku-shou-shen/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Git 仓库瘦身
  • 确保所有人的代码已提交且完成合并
  • Git 仓库最好只留下 matser 和 staging 两个分支 (或只有 matser 亦可)
  • 临时去掉 matser 和 staging 分支的保护 (操作完成后记得重新开启保护)
01.png

最开始我是直接使用 git-filter-branch 命令删除大文件的,但是实测发现该命令对 Github 有效;而 Gitlab 只能在本地瘦身,无法推送本地变更到服务器

查找占用空间最多的 top N 个文件

执行以下命令即可(其中 tail -30 表示最大的 top 30 文件,可根据实际情况修改):

git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -30 | awk '{print$1}')"

该命令会列出每个大文件的详细路径,后面用 BFG 工具删除时会用到的只有文件名,详细路径不需要。

02.png

裸克隆仓库镜像到本地

执行以下命令(注意 --mirror 参数是必须的):

git clone --mirror https://example.com/your-repo.git

使用 BFG 删除大文件

根据前面找到的大文件清单,逐个删除即可(把 big.file.name 替换成要删除的文件名):

cd your-repo.git
bfg --delete-files big.file.name --no-blob-protection
03.png

真正删除数据

执行完上一步之后,其实只是标记了哪些文件需要被删除而已,实际上这些文件还是存在于 git 的。

此时需要确实把这些文件删除掉,执行以下 git gc 命令:

git reflog expire --expire=now --all && git gc --prune=now --aggressive
04.png

推送变更到服务器

git push

参考文章:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK