惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Fortinet All Blogs
B
Blog
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
Y
Y Combinator Blog
Microsoft Azure Blog
Microsoft Azure Blog
L
LangChain Blog
Recent Announcements
Recent Announcements
U
Unit 42
Martin Fowler
Martin Fowler
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The Register - Security
The Register - Security
Recorded Future
Recorded Future
C
Check Point Blog
V
V2EX
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
Google DeepMind News
Google DeepMind News
酷 壳 – CoolShell
酷 壳 – CoolShell
F
Full Disclosure
小众软件
小众软件
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
Last Week in AI
Last Week in AI
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
爱范儿
爱范儿
P
Proofpoint News Feed
罗磊的独立博客
量子位
D
Docker
博客园_首页
D
DataBreaches.Net
Project Zero
Project Zero
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - Franky
Security Latest
Security Latest
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
Netflix TechBlog - Medium
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
博客园 - 三生石上(FineUI控件)
H
Hackread – Cybersecurity News, Data Breaches, AI and More
大猫的无限游戏
大猫的无限游戏

ABB00717

編譯器筆記 部落格聚集地 HTB - Busqueda 788. Rotated Digits 396. Rotate Function 幫耳機補血! 紀錄/週刊/ 做過的夢(旅行) 做過的夢 週刊 Vol.18 週刊 Vol.17 週刊 Vol.16 做過的夢(火箭推進器和追蹤導彈) 在 Ubuntu 24.04 中安裝 python2 和 pip2 動態牆 紀錄/音樂/ 用 miniflux 和 Cloudflare Tunnel 自架 RSS Reader 週記 Vol.15 關於用了 GCC 擴充功能,而被批評不夠 Clean Code 這檔事 GDB Makefile HTB - TwoMillion 週記 Vol.14 紀錄/Hack-The-Box/ 週記 Vol.13 ABB00717's Blog 1980. Find Unique Binary String 週記 Vol.12 紀錄/Leetcode/ 在互聯網上,什麼該說,什麼又不該說? 週記 Vol.8 週記 Vol.7 週記 Vol.6 天才之於一種義務 就算 LLM 能解答所有問題,你也不該放棄學習 Stack-Based Buffer Overflow 筆記/書籍/ 《絕佳時間》 週記 Vol.5 偽深刻的自我解構 筆記/Linux-雜項筆記/ 解決 Ubuntu 待機後喚醒異常的問題 將應用程式新增到 GNOME 的 Activities Overview 週記 Vol.4 Assembly Language 週記 Vol.3 筆記/ 文章/ 紀錄/ 資源/ 挑戰 週記 Vol.2 部落格該有的東西 週記 Vol.1 數學符號表 Advent of Code Day 8 Advent of Code Day 7 Obsidian 無痛轉成 Blog Advent of Code Day 6
清除 git history 中的機敏資料
2026-06-24 · via ABB00717

在歷史紀錄中定位憑證

在重寫歷史紀錄之前,先找出所有包含機敏資料的 commit。可以用 git log 來找:

git log -p -S "SECRET_STRING"

清理歷史紀錄

有兩種常見的方法來改寫 Git 歷史紀錄。現代標準是 git-filter-repo,但標準的 git filter-branch 在所有 Git 安裝中都可以用。

使用 git filter-branch

Tree filter 允許在每個 commit 的 checkout 過程中執行 shell 腳本。為了防止 git filter-branch 因為警告而停下等待,請設定 FILTER_BRANCH_SQUELCH_WARNING=1 環境變數。

建立一個 bash 腳本來將機密取代為預位符:

!/bin/bash
if [ -f path/to/file ]; then
  sed -i 's/SECRET_STRING/PLACEHOLDER/g' path/to/file
fi

設定可執行:

chmod +x clean.sh

從引入洩漏之 commit 的 parent 開始進行重寫(例如 INTRODUCED_COMMIT~1):

FILTER_BRANCH_SQUELCH_WARNING=1 git filter-branch -f --tree-filter /absolute/path/to/clean.sh INTRODUCED_COMMIT~1..HEAD

使用 git-filter-repo

現代工具 git-filter-repogit filter-branch 更安全快速。如果已經安裝,執行簡單的搜尋取代指令即可:

git-filter-repo --replace-text <(echo "SECRET_STRING==>PLACEHOLDER")

這會自動更新儲存庫歷史紀錄中的所有參照。

更新已 checkout 的分支

如果重寫是在臨時分支上進行的,其他分支(像是 master)仍然包含舊的 commit。請藉由將其指標重設為重寫後的 HEAD 來更新它們:

git checkout target-branch
git reset --hard rewritten-branch-or-commit

如果本地端有未 commit 的變更,建議先不要這麼做,因為 hard reset 會捨棄這些變更。

管理鎖定的分支與工作區 (Worktree)

如果 Git 因為分支已在 worktree 中 checkout 而拒絕刪除它,請找出該 worktree:

git worktree list

使用其路徑來移除 worktree:

git worktree remove /path/to/worktree

一旦 worktree 被移除,該分支就會被解鎖,並可以安全地刪除:

git branch -d branch-name

更新遠端儲存庫

如果重寫後的 commit 已經推送到遠端伺服器,標準的 push 會因為歷史紀錄分歧而失敗。必須強制推送以更新伺服器:

git push origin branch-name --force

請通知其他協作者,因為他們需要 fetch 並將其本地分支重設為新的歷史紀錄。