3

如何清除Git历史变更记录

 2 years ago
source link: https://shidawuhen.github.io/2022/05/08/%E5%A6%82%E4%BD%95%E6%B8%85%E9%99%A4Git%E5%8E%86%E5%8F%B2%E5%8F%98%E6%9B%B4%E8%AE%B0%E5%BD%95/
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.

以前公司的一个项目代码,从2014年一直存活到了2021年,目前应该还在被使用。整个代码将近2G,git文件极大,其中很大的一个原因是代码里存放过apk文件。如此大的项目,导致下载、发布很困难。

最初打算创建一个新的远程仓库用来替换旧仓库,但是该项目涉及的位置和使用者太多。另外一个方案是删除apk的变更记录,项目也会变小很多。此处推荐一个工具,能够较快的删除Git的历史变更记录。

一、创建仓库

创建远程仓库,里面有README.md文件,做两次提交,分别提交数字1和数字2。如下图所示:

image-20220508214256683

image-20220508214856403

二、安装工具

这款工具名为bfg,官方地址为:https://rtyley.github.io/bfg-repo-cleaner/。

image-20220508215136597

先下载jar文件到本地:

wget https://repo1.maven.org/maven2/com/madgag/bfg/1.14.0/bfg-1.14.0.jar

如果没有Java,则需要进行安装:

apt install default-jre
java -v

为了方便执行,我们可以设置别名:

alias bfg='java -jar bfg-1.14.0.jar'

1.删除文件

为了删除变更记录,需要先删除该文件。

image-20220508221946810

2.下载远程仓库

创建新的目录,将远程仓库下载下来

git clone --mirror git://example.com/some-big-repo.git
# git clone --mirror http://127.0.0.1:3000/shidawuhen/test.git

image-20220508221347849

3.删除指定文件

bfg --delete-files README.md test.git

通过日志能够看到删除情况:

image-20220508222211153

cd test.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push

通过git历史,发现sha1值有变更,而且里面的改动已经消失了

image-20220508222855230

image-20220508222542618

bfg的优势在于速度快,如果项目大是因为几个大文件导致的,大家可以用这种方案进行操作。

另外bfg的命令有很多,可以删除文件、可以根据文件大小等删除,我们可根据需求灵活使用。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK