X

一键替换包括历史git提交在内的所有包含文本(不留痕迹)

XFEstudio 2026-02-01 17:13 204
编辑于 2026-02-01 17:46

🟢 最推荐工具:git filter-repo(官方推荐)

git filter-branch 和 BFG 更安全、现代


具体操作&方案

🔴 重要前提(一定要做)

⚠️ 下面操作会改写历史
⚠️ 一旦推送,所有旧 commit hash 都会变
⚠️ 私有仓库 → 公共仓库,这是正确时机

👉 先备份仓库一份!


Step 1:克隆一个“操作用仓库”

git clone --mirror 你的私有仓库.git
cd 你的私有仓库.git

--mirror 会包含:

  • 所有分支
  • 所有 tag
  • 所有提交

Step 2:安装 git-filter-repo

pip install git-filter-repo

(Git ≥ 2.22 有时已内置)


Step 3:替换 / 删除 文本(核心)

🔹 如果你想要替换某个具体的 IP(比如 123.45.67.89

  1. 新建 replacements.txt
  2. replacements.txt 中写入你要替换的文本内容,如:
123.45.67.89==your.domain.com
  1. 在仓库根目录输入以下指令
git filter-repo --replace-text replacements.txt

或者直接删除(修改 replacements.txt ):

123.45.67.89

🔹 如果是多个 IP / 不确定

你可以在 replacements.txt 中使用正则:

regex:\b\d{1,3}(\.\d{1,3}){3}\b==REDACTED_IP

👉 所有效果:

  • 历史中所有 IPv4 地址 → REDACTED_IP

Step 4:检查是否真的干净了

git grep -nE '\b\d{1,3}(\.\d{1,3}){3}\b'

如果没有任何输出,说明历史里已经没 IP 了。


Step 5:推送到新的 GitHub 公共仓库

git remote remove origin
git remote add origin https://github.com/你的用户名/花再具箱.git
git push --force --all
git push --force --tags

⚠️ --force 是必须的(历史被改写)


0 条回复
暂无回复,快来抢沙发吧!
发表回复
登录 后发表回复。