从历史记录中清除文件

GitGitBeginner
立即练习

This tutorial is from open-source community. Access the source code

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

简介

Git 是一种流行的版本控制系统,它允许开发者跟踪代码库中的更改。然而,有时有必要从仓库历史记录中完全删除一个文件。本实验将指导你完成从 Git 历史记录中清除文件的过程。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/DataManagementGroup(["Data Management"]) git/DataManagementGroup -.-> git/filter("Apply Filters") subgraph Lab Skills git/filter -.-> lab-12747{{"从历史记录中清除文件"}} end

从历史记录中清除文件

假设你不小心将一个包含敏感信息(如 API 密钥或密码)的文件提交到了你的 Git 仓库中。你意识到这个文件本就不应该被提交,并且想要将它从仓库历史记录中彻底删除。然而,仅仅删除该文件并提交更改并不会将其从仓库历史记录中移除。在之前的提交中该文件仍然可以访问,这可能会带来安全风险。

要完成本实验,你将使用你 GitHub 账户中的 Git 仓库 git-playground,它是从 https://github.com/labex-labs/git-playground.git 派生而来的。这个仓库包含一个名为 file1.txt 的文件,它本就不应该被提交。请按照以下步骤从仓库历史记录中清除 file1.txt

  1. 将仓库克隆到你的本地机器:
git clone https://github.com/your-username/git-playground
  1. 使用以下命令导航到该目录并配置身份:
cd git-playground
git config --global user.name "your-username"
git config --global user.email "your-email"
  1. 从仓库索引中删除该文件。
git rm --cached --ignore-unmatch file1.txt
  1. 使用提交消息「删除敏感文件 file1.txt」提交此更改:
git commit -m "Remove sensitive file1.txt"
  1. 重写仓库历史记录,删除所有 file1.txt 的实例:
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch file1.txt" --prune-empty --tag-name-filter cat -- --all
  1. 将更改强制推送到远程仓库:
git push origin --force --all

完成这些步骤后,file1.txt 将被彻底从仓库历史记录中删除,并且在运行 git log --remotes 之后,你将不会在 file1.txt 上看到该提交记录。

总结

当敏感信息被意外提交时,从 Git 历史记录中清除文件是必要的步骤。本实验指导你使用 git filter-branch 命令从 Git 历史记录中清除文件的过程。请记住,在重写仓库历史记录时要谨慎,因为这可能会产生意想不到的后果。