从 git 存储库历史记录中删除文件

Aji*_*oel 8 git github

我错误地检入(到 github)一些敏感文件。为了解决这个问题,我按照这里的说明运行了命令:

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch settings.json" --prune-empty --tag-name-filter cat -- --all
echo "settings.json" >> .gitignore
git add .gitignore
git commit -m "Add settings.json to .gitignore"
git push origin --force --all
git push origin --force --tags
git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
Run Code Online (Sandbox Code Playgroud)

但是,我可以转到我的提交历史记录以查看已删除的文件。

要解决此问题,如何从 github 提交历史记录中删除该文件?

在此处输入图片说明

在此处输入图片说明

Von*_*onC 7

会转而使用最好的做法是使用新的工具,git filter-repo取代了BFG和git filter-branch

注意:如果您在运行上述命令时收到以下错误消息:

Error: need a version of `git` whose `diff-tree` command has the `--combined-all-paths` option`
Run Code Online (Sandbox Code Playgroud)

这意味着你必须更新git


参见“基于路径的过滤”:

git filter-repo --path settings.json --invert-paths
Run Code Online (Sandbox Code Playgroud)

然后 git push --force

最后不需要所有这些 repack/gc/prune:该工具会为您进行清理。