由于git仓库清理不可逆,会重写历史,所以我希望尽量都不会用到这个:
Git+Gerrit如何永久删除历史文件(大文件/私密文件)-腾讯云开发者社区-腾讯云
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网页上也有操作指导
二、扫描仓库
将超过一定大小的文件扫出来
#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.
1万+

被折叠的 条评论
为什么被折叠?



