Skip to main content

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

Updated 3 min readMarkdown
Part of seriesDevOps
How to Completely Remove Deleted Secret Files and Large Files from Git History

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 နဲ့ ပုံမှန် စစ်ပေးသင့်ပါတယ်

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

About the author

No comments yet. Be the first to comment.

More from this blog