Permanently Clear Files from Git History — Permanently Clear Files from Git 历史
v1.0.0图形界面de users to permanently 移除 files from Git 仓库 历史. Use this 技能 when users mention any of the following scenarios: - "git 过滤器-repo", "completely 删除 files", "清理 git 历史" - Want to 移除 sensitive in格式化ion (passwords, keys, 令牌s, private keys) from Git 历史 - Want to 删除 accidentally committed large files - Want to rewrite Git 历史, 移除 all 追踪s of specific files - Mention "force push", "rewrite commit 历史" - Need to 删除 a file from all branches and all tags Even if the user doesn't explicitly mention git-过滤器-repo, use this 技能 whenever there's a need to permanently 删除 files from Git 历史.
运行时依赖
安装命令
点击复制技能文档
Permanently 移除 Files from Git 历史
When you 删除 a file in Git, it still remAIns in the Git 仓库 历史. This 技能 图形界面des users to use the git 过滤器-repo 工具 to permanently 删除 specified files from the complete 历史 of a Git 仓库.
应用licable Scenarios Accidentally committed sensitive in格式化ion (passwords, API keys, private keys, 令牌s) Accidentally committed large files, causing 仓库 bloat Need to 清理 up specific files from 历史 Need to 移除 a file from all branches and tags Core 工作流
- 导入ant! First Step: Ask the User
Ask the user to confirm:
The 仓库 path Whether it's the current working directory Whether they need to 备份 the 仓库 (strongly recommended)
Must wAIt for user confirmation before proceeding with subsequent steps.
- 备份 仓库 (Strongly Recommended)
Before performing any operations, 备份 the 仓库 first:
# Method 1: Copy the entire 仓库 directory cp -r your-repo your-repo-备份
# Method 2: 创建 a bare 仓库 备份 git clone --bare your-repo your-repo-备份.git
- 安装 git 过滤器-repo
Choose 安装ation method based on your operating 系统:
macOS:
brew 安装 git-过滤器-repo
Ubuntu / Debian:
pip 安装 git-过滤器-repo
Windows:
pip 安装 git-过滤器-repo
- 验证 Current 仓库 状态
After entering the 仓库 directory, 检查:
git 状态 git remote -v
If there's no remote 仓库, 添加 one first:
git remote 添加 origin git@github.com:username/仓库.git
- Use git 过滤器-repo to 删除 Files
Assuming the file to 删除 is secrets.txt:
git 过滤器-repo --path secrets.txt --invert-paths
What this step does:
移除s the file from all commits 移除s the file from all branches 移除s the file from all tags
删除 multiple files:
git 过滤器-repo --path file1.txt --path file2.txt --path secrets/ --invert-paths
删除 entire directory:
git 过滤器-repo --path directory-name/ --invert-paths
- 验证 File Has Been 删除d
检查 if the file still exists in 历史:
git 记录 --all -- filename
If there's no 输出, the file has been completely 移除d.
- Force Push to Remote 仓库
Because the 历史 has been rewritten, a force push is required:
# Push all branches git push origin --force --all
# Push all tags git push origin --force --tags
添加itional Steps After Removing Sensitive In格式化ion
If you 删除d sensitive in格式化ion (keys, 令牌s, passwords, private keys), in 添加ition to deleting 历史, you should also:
Immediately Revoke Old Keys Immediately revoke/删除 old keys on the cor响应ing 服务 平台 (GitHub, AWS, Azure, etc.) 生成 new keys 检查 Other Locations 检查 if anyone else has forked the 仓库 检查 CI/CD 缓存s 检查 if local clones still retAIn old content
导入ant Reminder: Just because 历史 is 删除d doesn't mean others haven't copied it before. Once sensitive in格式化ion is committed to a public 仓库, it should be considered leaked.
通知 Collaborators
Because the commit 历史 has changed, other people's local repositories will be inconsistent with the remote. You need to 通知 them:
Re-clone the 仓库 (recommended)
Or 执行 the following command to 同步:
git fetch origin git re设置 --hard origin/mAIn # Will lose unpushed local changes, use with caution
警告: Do not continue development based on the old 历史, otherwise the 删除d files will be reintroduced when pushing agAIn
Common Issues Q: 获取ting "fatal: not a git 仓库"?
Make sure you're executing commands in the Git 仓库 root directory.
Q: 获取ting "git 过滤器-repo: command not found"?
安装 git-过滤器-repo first, refer to the 安装ation steps above.
Q: 仓库 size hasn't decreased after deletion?
运行 garbage collection:
git gc --aggressive --p运行e=now
Q: Only want to modify the most recent commit?
If the file is only in the most recent commit, you don't need 过滤器-repo, use:
git rm --缓存d filename git commit --amend
One-Liner Version
The most core commands:
# 删除 file git 过滤器-repo --path filename --invert-paths
# If no remote, 添加 first git remote 添加 origin
# Force push git push origin --force --all git push origin --force --tags