git修改之前提交的版本删除敏感数据

git里提交了敏感的数据,和文件,需要替换掉一些字符串,删除掉一些文件。

看有两种方法,用bfg或git filter-branch。


bfg简单试了下

删文件 java -jar bfg.jar --delete-files hello.php

替换字符串(未成功) java -jar bfg.jar --replace-text passwords.txt  --no-blob-protection


git filter-branch试了下

替换字符串 git filter-branch --tree-filter "find . -name 'hello.php' -exec sed -i -e 's/hello/world/g' {} \;"

删除行 git filter-branch --tree-filter "find . -name 'hello.php' -exec sed -i -e '/hello/d' {} \;"

删除文件 git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch -fr ./test/hello.php'


实际使用时,先clone下代码,用git filter-branch操作后,清理一下远程仓库再做push。

之前clone下的代码,需要删除了重新clone一份,看起来原因是用git filter-branch修改后commit hash版本号变了,之前clone的直接push上去后,会把旧的提交的版本push上去,这些就的提交里还包含那些敏感数据。


参考https://stackoverflow.com/questions/4110652/how-to-substitute-text-from-files-in-git-history


猜你喜欢

转载自blog.csdn.net/superzlc/article/details/79580023