之前的爬蟲(chóng)里面加了個(gè)發(fā)送郵件提示,綁了自己的個(gè)人郵箱和密碼,傳到git好久才發(fā)現(xiàn)這個(gè)問(wèn)題,但是如果手動(dòng)刪掉再發(fā)一個(gè)新的commit,歷史版本里仍然還會(huì)有這些信息,所以想要一個(gè)辦法清除所有commit里面的信息。
搜索了一下發(fā)現(xiàn)了一個(gè)工具。
BFG Repo-Cleaner
BFG Repo-Cleaner是一個(gè)專門用來(lái)清除git庫(kù)里bad data的工具。
主要有兩個(gè)功能
- Removing Crazy Big Files //刪掉某些文件
- Removing Passwords, Credentials & other Private data //刪除敏感信息
使用前注意本機(jī)需要安裝java.
此外在執(zhí)行之前先要提到一個(gè)我自己遇到的問(wèn)題
來(lái)自pull request的commit是不能被修改的
因?yàn)橥ㄟ^(guò)pull得到的commit是只讀的:
The refs beginning 'refs/pull' are synthetic read-only refs created by GitHub - you can't update (and therefore 'clean') them, because they reflect branches that may well actually come from other repositories - ones that submitted pull-requests to you.
如過(guò)你沒(méi)有這個(gè)問(wèn)題,那么我們繼續(xù)方法如下:
- 先到BFG主頁(yè)點(diǎn)擊頁(yè)面右側(cè)的下載按鈕下載BFG的
.jar文件. - 為了方便起見(jiàn)創(chuàng)建一個(gè)新文件夾,把剛剛下載的.jar復(fù)制進(jìn)去.
如果你是為了刪除個(gè)人密碼之類的信息,則在同一個(gè)文件夾下建立txt文件,每一行輸入一個(gè)你要?jiǎng)h除的敏感信息 - 打開(kāi)git bash, 跳轉(zhuǎn)到剛剛新建的文件夾,注意,語(yǔ)法為
cd /c/project/ - 復(fù)制你想要修改的庫(kù),鍵入:
$ git clone --mirror https://github.com/user/repo.git注意,這里若使用的是類似$ git clone --mirror git://example.com/some-big-repo.git的格式,在最后push的時(shí)候也會(huì)出現(xiàn)權(quán)限問(wèn)題,因?yàn)橐彩侵蛔x的.
參考1, 參考2
The problem is that URL like git://github.com/user/repo.git is read-only, SSH URL like git@github.com:user/repo.git and HTTPS URL like https://github.com/user/repo.git are writeable.
- 接下來(lái)使用BFG刪除txt文件里列出的敏感信息:
$ java -jar bfg.jar --replace-text password.txt my_repo到這一步的時(shí)候所有的歷史記錄就應(yīng)該被更新了 - 之后請(qǐng)進(jìn)執(zhí)行這段我也不知道是干什么但貌似是物理上清理掉剛剛被在記錄里更新掉但其實(shí)仍然可見(jiàn)的敏感信息的代碼
$ cd my_repo.git
$ git reflog expire --expire=now --all && git gc --prune=now --aggressive
- 最后,
$ git push,如果此處遇到了fatal: remote error:,則是上面提到的在復(fù)制時(shí)出現(xiàn)的問(wèn)題,請(qǐng)執(zhí)行$ git remote set-url origin <在這里改成HTTP開(kāi)頭的URL>來(lái)解決.
注意,如果你跟我一樣,repo里包含來(lái)自pull request的commit,那這些commit會(huì)被denied掉:
! [remote rejected] refs#11/head -> refs#11/head (deny updating a hidden ref)
目前沒(méi)找到解決辦法,刪庫(kù)重建吧 TAT