NEWS LETTER

GitHub单个文件脱敏处理

  • Home
  • git-filter-branch
Scroll down

最近往 github push 代码时不小心将 token 等敏感数据一起给提交到了仓库,如果是单次的话可以选择回退到上一次提交,或者还未 push 到仓库的情况下可以选择 git commit --amend,但是我后续很多次 commits 都未对这个文件进行脱敏处理,这种情况就会比较麻烦。

  • 遍历所有的 commit,删除指定文件,重写历史 commit
1
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch <file path>" --prune-empty --tag-name-filter cat -- --all
  • 本地记录覆盖到 Github (所有 branch 以及所有 tags)
1
2
git push origin --force --all
git push origin --force --tags
  • 强制解除对本地存储库中的所有对象的引用和垃圾收集
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
其他文章
请输入关键词进行搜索