日常提交代码的时候可能遇到的一个问题就是,不小心将一些数据库配置或者密钥文件提交到了线上,后面就算删除掉提交的文件,但是相关信息还是可以在历史版本中看到,下面介绍一下使用BFG Repo-Cleaner 来处理这种情况
BFG Repo-Cleaner
Removes large or troublesome blobs like git-filter-branch does, but faster. And written in Scala
BFG可以用来移除大文件或者处理我们想从 git history中的内容,就像GIt提供的git-filter-branch命令那样,不过比他更快。
步骤
1. 安装BFG
wget http://repo1.maven.org/maven2/com/madgag/bfg/1.12.16/bfg-1.12.16.jar
BFG需要在java环境执行
The Java Runtime Environment (Java 8 or above - BFG v1.12.16 was the last version to support Java 7)
2. clone一份镜像数据
bfg提供的破坏性操作一旦执行,后面就没办法找回之前的状态,所以为了以防万一,先备份
$ git clone --mirror git://example.com/some-big-repo.git
3. 替换敏感数据
$ java -jar bfg.jar --replace-text replacements.txt my-repo.git
- bfg.jar:下的什么版本这里就改成对应版本的bfg.jar,前文中使用wget下载了bfg-1.12.16.jar
- replacements.txt: 中填写需要替换的字段,可以使用正则匹配,默认使***REMOVED***代替原内容
- my-repo.git:本地git项目路径
Replace with ‘REMOVED’ (default)
PASSWORD1
# replace with 'examplePass' instead
PASSWORD2==>examplePass
#replace with the empty string
PASSWORD3==>
4. 删除缓存数据
$ git reflog expire --expire=now --all
$ git gc --prune=now
5. 提交本地修改到仓库
$ git push origin --force --all
$ git push origin --force --tags
- –all参数是作用于所有分支上
6. 更新其他的clone
如果其他服务器或者电脑上clone了该仓库的代码,简单使用git pull是无法更新前文中的修改的
这里用到的命令是:
$ git fetch origin
$ git reset --hard origin/master
然后同样用一下命令删除一下缓存信息
$ git reflog expire --expire=now --all
$ git gc --prune=now