2024-03-04 00:05阅读: 274评论: 0推荐: 0

[ 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

本文作者:YEUNGCHIE

本文链接:https://www.cnblogs.com/yeungchie/p/18051025

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   YEUNGCHIE  阅读(274)  评论(0编辑  收藏  举报
评论
收藏
关注
推荐
深色
回顶
收起

感谢支持

扫描二维码打赏

微信打赏

点击右上角即可分享
微信分享提示