[ Git ] 不留痕迹地删除某个文件

https://www.cnblogs.com/yeungchie/

有时候不小心 commit 了一个包含敏感信息的文件,只是用 git rm 来删除还是会保留一个提交历史,查看版本就能把源文件提取出来。

下面演示如何解决这个问题,例如删除当前路径下的 README.md 文件。

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch README.md' --prune-empty --tag-name-filter cat -- --all

这一步耗时会比较久,报一个 WARNING:

WARNING: git-filter-branch has a glut of gotchas generating mangled history
         rewrites.  Hit Ctrl-C before proceeding to abort, then use an
         alternative filtering tool such as 'git filter-repo'
         (https://github.com/newren/git-filter-repo/) instead.  See the
         filter-branch manual page for more details; to squelch this warning,
         set FILTER_BRANCH_SQUELCH_WARNING=1.
Proceeding with filter-branch...

告诉你会破坏历史记录 balabala,我就是要破坏,不管。

推送的时候要加 --force

git push --force

这一步需要注意远程受保护的分支可能会推送失败,需要在服务端临时修改一下,允许强制推送。

image

posted @ 2024-03-04 00:05  YEUNGCHIE  阅读(124)  评论(0编辑  收藏  举报