github删除commit记录里的密码

38 阅读1分钟

背景

公开repo的时候会担心曾经有不小心commit的密码能在commit历史里被翻出来。所以可以用BFG Repo-Cleaner by rtyley把历史记录里的密码替换成REMOVED。

image.png

steps

  1. download bfg.jar from repo1.maven.org/maven2/com/…
  2. write your passwords in passwords.txt
username1
password1

username2
password2
  1. replace history and push
git clone --mirror git://xxx.com/xxx.git
java -jar bfg.jar --replace-text passwords.txt  xxx.git
cd xxx.git
git reflog expire --expire=now --all && git gc --prune=now --aggressive
git push --force

如果这个push成功了就大功告成了。

失败的话可能是[remote rejected] refs/pull/xxx报错,这是因为git clone --mirror会把pull request 也clone下来,而github目前不能直接修改pr,需要联系support人员才可能实现,所以push失败,参考Delete a closed pull request from GitHub - Stack Overflow。如果只是git clone,不用mirror,可以修改main分支的密码成功,但pr history密码还在。如果想彻底消除影响就还是得新建一个repo或者改密码。