在使用GitHub的过程中,假如某次提交代码时不小心将敏感信息
提交进了公共仓库。
如果发现得及时,本地提交后还没有推送到GitHub远程仓库的话,这种情况还好处理,直接修改代码后通过git commit --amend
即可。
但如果发现时已经推送到了GitHub远程仓库,或者已过了许久,后续有了很多新的commits,这种情况就会比较复杂了。
处理方式:git filter-branch
1、删除本地记录
1
|
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch 你要删除的文件(相对项目的路径)" --prune-empty --tag-name-filter cat -- --all
|
1
|
git push origin --force --all
|
1
|
git push origin --force --tags
|
3、确保没有什么问题之后,强制解除对本地存储库中的所有对象的引用和垃圾收集
1
2
3
|
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
|
注:执行第一步时报错:
Cannot rewrite branches: You have unstaged changes.
解决方案:
出现以下提示信息
1
2
3
4
5
6
7
8
|
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: xxx
no changes added to commit (use "git add" and/or "git commit -a")
|
1
2
3
|
git checkout -- <file>
或
git add <file>
|
解决!!!
切记,项目开源时请记得脱敏
!!!