git仓库清理

由于git仓库清理不可逆,会重写历史,所以我希望尽量都不会用到这个:

先收藏:git仓库清理--"保姆级"教程 - 掘金

 Git+Gerrit如何永久删除历史文件(大文件/私密文件)-腾讯云开发者社区-腾讯云

解决.git目录过大问题 | Escape

git bfg 的使用(删除提交记录中的敏感信息,删除文件等)-CSDN博客

尝试着清理过一次仓库,会有很大的问题。

注意要注意的问题:

1.仓库清理后开发本地同步代码需要使用git pull --rebase而不是git pull。否则会有问题。

因为git清理改写了远程仓库

2.开发在git push前要git pull --rebase一下,否则又会把大文件提交上来。

3.在进行正式的清理前,本地多演练几遍,仓库也做好备份。

 我参照着网上的方法进行清理的时候发现最终本地清理完的仓库无法同步到远程gerrit平台上。

即gerrit服务器上存放的.git文件夹大小没有变化。

所以我最后的方案是在gerrit服务器上进行操作的,使用的是bfg.jar工具,这个工具计算起来特别快,比git filter-branch命令快n倍,而且有详细的日志。

一、下载bfg.jar

BFG Repo-Cleaner by rtyley

bfg网页上也有操作指导

二、扫描仓库

将超过一定大小的文件扫出来

#clone一份远程仓库的copy到本地
git clone --mirror git://example.com/some-big-repo.git
#将超过100M的文件进行清理,bfg会重写所有的commits和branch以及tags
java -jar bfg.jar --strip-blobs-bigger-than 100M some-big-repo.git

三、清理仓库

Examine the repo to make sure your history has been updated, and then use the standard git gc command to strip out the unwanted dirty data, which Git will now recognise as surplus to requirements:

检查历史是否被改写,用git gc清理不可达的数据。

$ cd some-big-repo.git
$ git reflog expire --expire=now --all && git gc --prune=now --aggressive

四、推送

最后一步是git push,但由于我是直接在gerrit服务器进行的操作,所以我是直接改写了,不需要git push。

Finally, once you're happy with the updated state of your repo, push it back up (note that because your clone command used the --mirror flag, this push will update all refs on your remote server):
git push

提醒

At this point, you're ready for everyone to ditch their old copies of the repo and do fresh clones of the nice, new pristine data. It's best to delete all old clones, as they'll have dirty history that you don't want to risk pushing back into your newly cleaned repo.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值