三亩地 三亩地SAN MU DI · CODE DIARY
ARTICLE DETAIL

日记详情

真实记录编程学习的某一天,欢迎挑你感兴趣的翻一翻。

使用git filter-repo删除已提交到git中的敏感信息,api key,配置文件等

使用git filter-repo删除已提交到git中的敏感信息,api key,配置文件等

使用git filter-repo删除已提交到git中的敏感信息,api key,配置文件等

前提条件

Python 3.5+

git >= 2.22.0

通过 pip 安装:pip install git-filter-repo

注意事项

官方推荐在fresh clone上修改,即clone一份远程的再做修改

操作后所有协作者必须重新 clone

因为历史被重写,SHA 全部改变,不能直接 pull

安装pypthon

在官网下载

安装时注意勾选☑️Add python.exe to PATH(最重要,默认不勾)

验证是否安装成功

python--versionpip--version

pip是 Python 的包管理工具,用来安装第三方库。

类比ava 的Maven/Gradle,Node.js 的npm

安装git filter-repo

pipinstallgit-filter-repo

删除api key

这里以一个openssl生成的密钥为例

在spring项目中我们会使用application.yml来配置密钥

有时会不小心把本地开发的密钥push上去

password:3afRJQRWzda+i5JRSYx3SzZTMCs0szypM4vT2/qDE6Y

我们可以将此密钥替换为YOUR_PASSWORD

在项目中新建一个replacements.txt文件

里面的内容为

3afRJQRWzda+i5JRSYx3SzZTMCs0szypM4vT2/qDE6Y==>YOUR_PASSWORD

执行以下git命令,用filter之后要重新添加远程仓库后再push

gitfilter-repo --replace-text replacements.txtgitremoteaddorigin 你的仓库地址gitpush origin--force--all

删除敏感文件

执行以下git命令

gitfilter-repo--path文件路径 --invert-paths--forcegitremoteaddorigin 你的仓库地址gitpush origin--force--all

rt-paths --force
git remote add origin 你的仓库地址
git push origin --force --all

← 返回列表