หลังจากที่ได้ Review Code พบว่าบางส่วนมันหลดไป แล้วมี Endpoint + API Key ติดไปด้วย ที่ต้องเอาออกไป ถ้าหลุดไปเดี๋ยวยาว ลองมาปัดฝุ่นดู เราเคยใช้ BFG Repo-Cleaner เมื่อหลายปีก่อนลบพวกนี้ในงานอยู่แล้ว แต่มันจดๆไว้ใน Notion รอบนี้เลย ย้ายมาลง Blog เลยดีกว่า
ทำไมต้องใช้ BFG Repo-Cleaner
สั่่นๆ Simple แล้วตัว Tools นี้มันมี Feature พวกนี้มาให้เลย ถ้าใช้ git-filter repo น่าจะท่ายากไปนะ ถ้าพร้อมแล้วไป Download BFG Repo-Cleaner และติดตั้ง Java 8++ กันได้เลย คอมผมใช้ Java 21 ทำงานได้นะ
ลุยกันเลย
- Backup Your Repository - ก่อนจะทำอะไรพวกนี้ แล้วถ้ามีงานที่คนอื่นต้องเกี่ยวข้องด้วย ต้องแจ้งทีม อันนี้ mirror ออกมาไว้ หรือ ท่าปกติ Zip ลงคอมก็แล้วก็ได้ครับ
git clone --mirror https://github.com/your/repo.git your-repo-backup.git
- Create Replace File -อันนี้ผมจะลบตัว Endpoint + API Key สร้างไฟล์ bfg-replace.txt ข้างในให้มีเนื้อหาประมาณนี้ครับ
myendpoint.com==>REDACTED THIS_IS_MY_SECRET_KEY==>REDACTED_API_KEYมาดู
- ก่อนจะเริ่มกัน มาดูก่อนก่อน ว่าผมวางของไว้ที่ไหนครับ base path เป็นยังไง
MyProject |- bfg-1.14.0.jar |- MyProjectCode | |-SomefilethatContainSecret.vue |- bfg-replace.txt
- Run bfg-1.14.0.jar โดยรูปแบบ Pattern คำสั่งประมาณนี้ และตัวอย่างที่ใช้จริง
cd /path/to/your/repo java -jar /path/to/bfg.jar --replace-text /path/to/bfg-rep
cd D:\MyProject\MyProjectCode java -jar D:\MyProject\bfg-1.14.0.jar --replace-text D:\MyProject\bfg-replace.txt
- หลังจาก Runไปแล้ว มันมี Log ตามนี้เลย
PS D:\MyProject\MyProjectCode> java -jar D:\MyProject\bfg-1.14.0.jar --replace-text D:\MyProject\bfg-replace.txt Using repo : D:\MyProject\MyProjectCode\.git Found 29 objects to protect Found 2 commit-pointing refs : HEAD, refs/heads/master Protected commits ----------------- These are your protected commits, and so their contents will NOT be altered: * commit 594c0463 (protected by 'HEAD') - contains 1 dirty file : - server/api/issues/issue-version.ts (1.7 KB) WARNING: The dirty content above may be removed from other commits, but as the *protected* commits still use it, it will STILL exist in your repository. Details of protected dirty content have been recorded here : D:\MyProject\MyProjectCode.bfg-report\2025-01-01\05-50-19\protected-dirt\ If you *really* want this content gone, make a manual commit that removes it, and then run the BFG on a fresh copy of your repo. Cleaning -------- Found 9 commits Cleaning commits: 100% (9/9) Cleaning commits completed in 130 ms. Updating 1 Ref -------------- Ref Before After --------------------------------------- refs/heads/master | 594c0463 | 92a4bf56 Updating references: 100% (1/1) ...Ref update completed in 50 ms. Commit Tree-Dirt History ------------------------ Earliest Latest | | . . . . . D D D m D = dirty commits (file tree fixed) m = modified commits (commit message or parents changed) . = clean commits (no changes to file tree) Before After ------------------------------------------- First modified commit | 3bc91701 | 167c0c06 Last dirty commit | 70502b32 | 67d8eee9 Changed files ------------- Filename Before & After ---------------------------------- index.get.ts | 5f39ee17 โ’ 0f98dd2d In total, 10 object ids were changed. Full details are logged here: D:\MyProject\MyProjectCode.bfg-report\2025-01-01\05-50-19 BFG run is complete! When ready, run: git reflog expire --expire=now --all && git gc --prune=now --aggressive
- Check Code อาจจะลองรัน App / Unit Test
- Clean Up - เอา Reflog ของ Git ที่ไม่จำเป็นออกไป เพราะมันไปแก้ Commit เดิม และ Clean GC ออก
git reflog expire --expire=now --all git gc --prune=now --aggressive
- Force Push
git push origin --force --all git push origin --force --tags
- ถ้าไปดู Code ใน Git มันจะ Replace เป็นคำที่เราต้องการให้ แล้วส่อง History จะไม่เป็นค่าเดิมด้วยครับ

Discover more from naiwaen@DebuggingSoft
Subscribe to get the latest posts sent to your email.