Skip to main content

Command Palette

Search for a command to run...

How to Completely Remove Deleted Secret Files and Large Files from Git History

Updated
3 min readView as Markdown
How to Completely Remove Deleted Secret Files and Large Files from Git History
S

Site Reliability Engineer @ onenex.co

Git ဆိုတာ အားလုံးလည်းသိကြတဲ့, အရမ်းအသုံးဝင်တဲ့ version control system တစ်ခုပဲ ဖြစ်ပါတယ်။ ဒီခေတ်မှာ git မသုံးတဲ့ developer ဆိုတာလည်း မရှိတော့သလောက်ပါပဲ။​ Git repository ထဲကို ဖိုင်တစ်ဖိုင် add ပြီး commit လိုက်တာနဲ့ အဲ့ဖိုင်ကို ပြန်ဖျက်လိုက်ရင်တောင် histroy ထဲမှာ အမြဲတမ်းကျန်နေခဲ့မှာပါ။ ဒါကြောင့် git ကို time machine လို့ တစ်ချို့က တင်စားပြီးခေါ်ကြတာပါ။​ ဒီအရမ်း အသုံးဝင်တဲ့ time machine ထဲကို credentials ဖိုင်တွေ (e.g: .env, private keys)နဲ့ ဖိုင်ဆိုဒ် အရမ်းကြီးပြီး repository ထဲ ထည့်စရာမလိုတဲ့ ဖိုင်တွေကို မတော်တဆ ထည့်မိရင်တော့ ပြဿနာရှိလာပါပြီ။

ဒီဆောင်းပါးမှာတော့ git history ထဲကို မတော်တဆ commit ထဲ ထည့်မိပြီးမှ ပြန်ဖျက်ထားခဲ့တဲ့ credentials ဖိုင်တွေနဲ့ file size ကြီးတဲ့ဖိုင်တွေကို အပြီးအပြတ် ဖယ်ရှားနိုင်မယ့်နည်းလမ်းတွေကို ဘယ်လို အသုံးပြုရမလဲ ပြောပြပေးသွားပါမယ်။

🤔 Why Just Deleting the File Isn’t Enough?

ဖိုင်တစ်ခုကို git rm path/to/secret.file နဲ့ ဖျက်ပြီး commit လုပ်လိုက်တာနဲ့ git history ထဲက ပျက်မသွားပါဘူး။ ဘယ်သူမဆို ဖိုင်မဖျက်ခင်အခြေအနေကို git checkout နဲ့ ပြန်သွားယူလို့ရပါတယ်။

git checkout <commit-hash> -- path/to/secret.file

ဒါကြောင့် ဒီဖိုင်ကို history ထဲက ဖျက်လိုက်ချင်ရင် ဒီဖိုင် စထည့်တဲ့ commit ကနေစပြီး git history တစ်ခုလုံးကို ပြန်ရေးဖို့လိုပါတယ်။

🛠️ Tools You Can Use

📏 Git Repo Size ကို ကြည့်ဖို့ git-sizer အသုံးပြုနိုင်ပါတယ်။

🔧 Installation
# MacOS
brew install git-sizer

# Ubuntu
sudo apt install git-sizer
🧪 Run
git-sizer

Processing blobs: 41369                        
Processing trees: 89219                        
Processing commits: 3589                        
Matching commits to trees: 3589                        
Processing annotated tags: 5                        
Processing references: 39                        
| Name                         | Value     | Level of concern               |
| ---------------------------- | --------- | ------------------------------ |
| Biggest objects              |           |                                |
| * Blobs                      |           |                                |
|   * Maximum size         [1] | 962.0 MiB | ******                         |
|                              |           |                                |
| Biggest checkouts            |           |                                |
| * Maximum path depth     [2] |    16     | *                              |
| * Maximum path length    [3] |   144 B   | *                              |

[1]  cabe5dd7cf7e0c275f789bd64cc5a943a7b79acb (refs/remotes/origin/example-branch:storage/path/large-file.ext)
[2]  9a7d193d8ea79aa0a21b0a8e13310c63cb54dc83 (refs/remotes/origin/example-branch^{tree})
[3]  600bc870aee0d3e4a97f592c8fd4b92edbf70ab4 (7ac90183211e4e4832650b03fa296773e5002ca8^{tree})

git-sizer ကို သုံးပြီး ဘယ် commits တွေကြီးနေတယ်၊ ဘယ်ဖိုင်အကြီးတွေ history ထဲပါနေတယ် ဆိုတာတွေကို သိရှိနိုင်ပါတယ်။ git repo size ကြီးအောင် ဘယ်ကောင်တွေက နေရာယူနေလဲ သိပြီဆိုရင်၊ အဲ့ဖိုင်တွေကို history ထဲကနေ အောက်ပါ နည်းလမ်းတွေသုံးပြီး ဖျက်လို့ရပါတယ်။

🧹 Step 2: Remove Files from Git History

Method 1: Using git filter-repo

✅ Install
brew install git-filter-repo
# or
pipx install git-filter-repo

filter-repo ကို install လုပ်ပြီးပြီဆိုရင်တော့ တစ်ဖိုင်ခြင်းဖြစ်စေ၊ တစ်ဖိုင်ထက် ပိုပြီးတော့ဖြစ်စေ ရှင်းနိုင်သလို။ extension နဲ့လည်း history ထဲက ရှင်းလိုက်လို့ရပါပြီ။

🧼 Remove Specific Files
git filter-repo --path .env --invert-paths

Multiple files:

git filter-repo --path .env --path secrets.json --invert-paths

By extension:

git filter-repo --path-glob '*.zip' --invert-paths

Method 2: Using BFG Repo-Cleaner

filter-repo ထက်စာရင် bfg က သုံးရတာ ပိုလွယ်ပါတယ်။ --delete-files option ကို သုံးပြီး ရှင်းကြသလို --strip-blobs-bigger-than option ကိုသုံးပြီးလည်း file size limit နဲ့လည်း ရှင်းလို့ရပါတယ်

✅ Install
brew install bfg
💥 Remove Files or Blobs
# Remove sensitive files
bfg --delete-files .env

# Remove blobs bigger than 100MB
bfg --strip-blobs-bigger-than 100M

History ထဲကနေ ဖျက်ပြီးပြီဆိုရင်တော့ မလိုတော့တဲ့ reflog တွေကို ဖျက်ပစ်ပြီး၊ မသုံးတော့တဲ့ object တွေကို ဖျက်ပစ်ဖို့ အောက်က command တွေ run ပေးဖို့လိုက်ရင်တော့ git history ထဲက ဖိုင်အကြီးတွေ မရှိတော့တဲ့အတွက် .git folder size က အများကြီး ကျသွားတာကို တွေ့ရပါလိမ့်မယ်။

🔄 Final Cleanup
git reflog expire --expire=now --all
git gc --prune=now --aggressive
git push --force-with-lease

⚠️ Important Warnings

  • ပြီးရင်တော့ force pushing နဲ့ပြန်တင်ပေးဖို့လိုသလို collaborators တွေကလည်း repo ကို ဖျက်ပြီး clone ပြန်လုပ်တာဖြစ်စေ hard reset လုပ်တာဖြစ်စေ လုပ်ပေးဖို့လိုပါတယ်။

  • ဒါတွေကို မလုပ်ခင်မှာ repository backup လုပ်ထားသင့်ပါတယ်။


🔐 Prevent Future Leaks

  • ဒီလိုမျိုးတွေ နောက်တစ်ခါ commit မလုပ်မိအောင် .gitignore ထဲမှာ ထည်ပေးထားသင့်ပါတယ်။

  • file အကြီးတွေ history ထဲပါနေလား သိရအောင် weekly လောက်ဖြစ်ဖြစ် git-sizer နဲ့ ပုံမှန် စစ်ပေးသင့်ပါတယ်

အဆုံးအထိ ဖတ်ပေးလို့ ကျေးဇူးတင်ပါတယ်။

More from this blog

Difference Between Regional NAT Gateway and Zonal NAT Gateway

ဒီ post လေးထဲမှာတော့ Regional NAT Gateway နဲ့ Zonal NAT Gateway ကွာခြားချက်တွေကို နှိုင်းယှဉ်ပြသွားမှာဘဲ ဖြစ်ပါတယ်။ ပထဆုံးအနေနဲ့ NAT Gateway ဆိုတာ ဘာလဲ ဘာအတွက် လိုအပ်တာလဲဆိုတာကို အရင်ပြောပြပေးပါမယ်။ P

Jun 19, 20264 min read132
Difference Between Regional NAT Gateway and Zonal NAT Gateway

Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင် ဒါမျိုး Alarms လုပ် 🔥🔥🔥

High Level ရေးထားတာပါ ဒါပေမဲ့ လွယ်ပါတယ် ​ကိုယ့်မှာ AWS Infra တွေရှိတယ်ဆို တွေ့သမျှ metric တွေကို alarms တွေလုပ်ပြီး notification ယူမနေဘဲ တကယ် effective ဖြစ်တဲ့ metric တွေကိုမှ CloudWatch ရဲ့ alarm feature တွေနဲ့ ပေါင်းပြီး ပို့စေချင်ပါတယ်။ ​ဥပမာ prod...

Jan 17, 20263 min read253
Infrastructure ကိုင်ပြီး အိပ်ရေးမပျက် ချင် လျှင်  ဒါမျိုး Alarms လုပ် 🔥🔥🔥

How to connect On Premises Network and Cloud (AWS)? (Part-2)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ဘယ်လို ချိတ်ဆက်ကြမလဲ? အပိုင်း (၂) မှာ တော့ Direct connect အကြောင်းကို ဆွေးနွေး သွားမှာ ဖြစ်ပါတယ်။ အပိုင်း (၁) Site-to-site VPN အကြောင်းကို လေ့လာချင်ရင်တော့ အောက်ပါ link မှာ ...

Dec 20, 20253 min read280
How to connect On Premises Network and Cloud (AWS)? (Part-2)

How to connect On Premises Network and Cloud (AWS)? (Part-1)

ကိုယ့်ရဲ့ ‌data center (on-prem) network နဲ့ AWS ချိတ်ဆက်ဖို့ လိုလာပြီဆိုရင် ချိတ်ဆက်နိုင်တဲ့ နည်း (၂) နည်း ရှိပါတယ်။ 1. Site-to-Site VPN (Virtual Private Network) 2. Direct connect Site-to-Site VPN - On-prem network နဲ့ AWS resources တွေ ချိတ်ဆက်တဲ့...

Dec 12, 20252 min read312
How to connect On Premises Network and Cloud (AWS)? (Part-1)
M

Myanmar Technical Blog

110 posts

Cloud, Linux, DevOps, Docker, Security အစရှိတဲ့ နည်းပညာများ အကြောင်းကို မြန်မာလို ပြန်လည်မျှဝေပေးမယ့် Blog ပဲဖြစ်ပါတယ်ခဗျာ...