การ Commit ไฟล์ขนาดใหญ่ (large files) เข้า Git Repository เป็นปัญหาทั่วไปที่นักพัฒนาต้องเผชิญ หากไม่จัดการไฟล์ขนาดใหญ่อย่างเหมาะสม Git จะ Clone ช้า Repository นี้จะมีขนาดใหญ่โต บทความนี้อธิบายวิธีการบริหารไฟล์ขนาดใหญ่ใน Git อย่างถูกต้อง เพื่อให้ Repository ของ ผู้ให้บริการโฮสติ้ง Cloud VPS ของคุณทำงานได้อย่างมีประสิทธิภาพ
ปัญหาของไฟล์ขนาดใหญ่ใน Git
ไฟล์ขนาดใหญ่คือไฟล์ที่มีขนาด 100 MB หรือมากกว่า เช่น วิดีโอ ไฟล์ภาพความละเอียดสูง ข้อมูล Dataset หรือไฟล์บายนารี่ หากทำการ Commit โดยไม่รู้ตัว จะทำให้เกิดปัญหาต่อไปนี้:
- Repository ขนาดใหญ่ขึ้นอย่างรวดเร็ว ทำให้ Clone ช้าลง
- Git History ขยายตัว ทำให้การดึงข้อมูลช้า
- ทีม Collaboration ต้องใช้เวลา Clone Repository บ่อยครั้ง
- ความปลอดภัยลดลง เพราะไฟล์ขนาดใหญ่อยู่ใน History
- CI/CD Pipeline ช้า เพราะต้องประมวลผลไฟล์ขนาดใหญ่
ทำไม Git ไม่ควรเก็บไฟล์ขนาดใหญ่
Git ถูกออกแบบมาเพื่อเก็บโค้ดข้อความ ไม่ใช่ไฟล์ขนาดใหญ่ เมื่อ Commit ไฟล์ขนาดใหญ่ Git จะจัดเก็บทั้งไฟล์และประวัติทั้งหมด ซึ่งทำให้ Repository ขยายตัวอย่างรวดเร็ว
วิธีที่ 1: ใช้ git filter-repo (แนะนำ)
วิธีนี้เป็นวิธีใหม่และแนะนำมากกว่า filter-branch เพราะทำงานได้เร็วและง่ายกว่า
# ติดตั้ง git filter-repo (macOS)
brew install git-filter-repo
# ติดตั้งบน Linux
pip install git-filter-repo
# ลบไฟล์ .zip ทั้งหมดออกจาก History
git filter-repo --path '*.zip' --invert-paths
# ลบไฟล์ขนาดใหญ่ที่มีขนาดมากกว่า 100MB
git filter-repo --strip-blobs-bigger-than 100M
# ลบไฟล์ประเภทต่างๆ
git filter-repo --path '*.mp4' --invert-paths
git filter-repo --path '*.iso' --invert-paths
# ลบหลาย pattern เพื่อให้ Cleaner
git filter-repo --invert-paths --path '*.mp4' --path '*.iso' --path '*.zip'
วิธีที่ 2: ใช้ git filter-branch (วิธีเก่า)
# ลบไฟล์ .zip ออกจาก History
git filter-branch --tree-filter 'rm -f *.zip' -- --all
# ลบไฟล์ที่มีชื่อเฉพาะ
git filter-branch --tree-filter 'rm -f large-data.bin' -- --all
# Cleanup refs
rm -rf .git/refs/original/
git gc --aggressive --prune
git gc --aggressive --prune=now
วิธีที่ 3: ใช้ BFG Repo Cleaner
# ติดตั้ง BFG
brew install bfg
# ลบไฟล์ที่มีขนาดมากกว่า 100MB
bfg --strip-blobs-bigger-than 100M
# ลบไฟล์โดยชื่อ
bfg --delete-files large-data.bin
# Cleanup
git reflog expire --expire=now --all
git gc --aggressive --prune=now
Git LFS (Large File Storage)
Git LFS เป็น Extension ของ Git ที่ช่วยจัดเก็บไฟล์ขนาดใหญ่อย่างมีประสิทธิภาพ โดยเก็บเพียง Pointers ในขณะที่ไฟล์จริงเก็บไว้ใน External Storage
ติดตั้ง Git LFS
# ติดตั้งบน macOS
brew install git-lfs
# ติดตั้งบน Linux (Ubuntu/Debian)
sudo apt-get install git-lfs
# ติดตั้งบน CentOS/RHEL
sudo yum install git-lfs
# เริ่มใช้ Git LFS
git lfs install
# ตรวจสอบการติดตั้ง
git lfs version
Track ไฟล์ประเภทต่างๆ ด้วย Git LFS
# Track ไฟล์ Photoshop
git lfs track "*.psd"
# Track ไฟล์วิดีโอ
git lfs track "*.mp4"
git lfs track "*.mov"
git lfs track "*.avi"
# Track ไฟล์ประเภท Archive
git lfs track "*.zip"
git lfs track "*.tar.gz"
git lfs track "*.iso"
# Track ไฟล์ Database
git lfs track "*.sql"
git lfs track "*.db"
# Track ไฟล์ Machine Learning
git lfs track "*.h5"
git lfs track "*.pkl"
git lfs track "*.model"
# Commit .gitattributes
git add .gitattributes
git commit -m "Configure Git LFS for large files"
# ดูรายการไฟล์ที่ Track
cat .gitattributes
ย้ายไฟล์ที่มีอยู่แล้วไปยัง Git LFS
# ย้ายไฟล์ .zip ที่มีอยู่แล้วไปยัง LFS
git lfs track "*.zip"
git add .gitattributes
git rm --cached *.zip
git add *.zip
git commit -m "Migrate .zip files to Git LFS"
# ผลักไปยัง Remote
git push origin main
การใช้ .gitignore เพื่อป้องกัน Commit ไฟล์ขนาดใหญ่
สิ่งสำคัญคือป้องกันไม่ให้ Commit ไฟล์ขนาดใหญ่ตั้งแต่เริ่มต้น โดยใช้ .gitignore
# .gitignore - ป้องกันไฟล์ขนาดใหญ่
# Build artifacts
build/
dist/
*.o
*.a
*.so
# Large files
*.zip
*.tar.gz
*.iso
*.dmg
*.exe
*.msi
# Video and media files
*.mp4
*.mov
*.avi
*.mkv
*.flv
*.wmv
# Audio files
*.mp3
*.wav
*.flac
# Database files
*.sql
*.db
*.sqlite
*.backup
# Machine learning models
*.h5
*.pkl
*.joblib
*.model
# Large datasets
data/
datasets/
*.csv
*.parquet
# Log files
*.log
logs/
# OS files
.DS_Store
Thumbs.db
.vscode/
.idea/
# Temporary files
*.tmp
*.temp
*.bak
Git LFS Workflow ที่สมบูรณ์
# 1. สร้าง Project ใหม่
mkdir my-project && cd my-project
git init
git lfs install
# 2. กำหนด Large File Types สำหรับ LFS
git lfs track "*.psd"
git lfs track "*.zip"
git lfs track "*.mp4"
# 3. Commit .gitattributes
git add .gitattributes
git commit -m "Configure Git LFS for large files"
# 4. เพิ่ม Large File
cp /path/to/large-file.psd .
git add large-file.psd
git commit -m "Add design file"
# 5. ตั้งค่า Remote
git remote add origin https://github.com/user/my-project.git
# 6. Push ไปยัง Remote
git push -u origin main
# 7. ตรวจสอบสถานะ LFS
git lfs ls-files
# ผลลัพธ์:
# 6bce3a5c34 * large-file.psd
# abcdef1234 * another-video.mp4
GitHub / GitLab LFS Quota
เมื่อใช้ GitHub หรือ GitLab ให้ระวังเกี่ยวกับ LFS Quota (ความจุ):
- GitHub Free: 1 GB Free Data Transfer, 1 GB Storage
- GitHub Pro: 100 GB Data Transfer, 1 GB Storage (ต้องซื้อเพิ่มเติม)
- GitLab Free: 10 GB LFS Storage
- GitLab Premium: Unlimited LFS Storage
# ตรวจสอบขนาด LFS ที่ใช้
git lfs ls-files | awk '{ sum += $4 } END { print sum / 1024 / 1024 / 1024, "GB" }'
# ดู LFS Objects Info
git lfs migrate info --top=10
Best Practices สำหรับ Large Files Management
- วางแผนตั้งแต่เริ่มต้น: เตรียม .gitignore และตั้งค่า Git LFS ก่อนเริ่ม Project
- ใช้ .gitattributes: ตั้งค่า LFS สำหรับประเภทไฟล์ที่รู้ว่าจะเป็นขนาดใหญ่
- Documentation: เขียนให้ชัดว่าไฟล์ประเภทใดใช้ Git LFS
- CI/CD: ตั้งค่า CI/CD ให้ดาวน์โหลดไฟล์ LFS ตามต้องการ
- Monitor Size: ตรวจสอบขนาด Repository เป็นประจำ
- Cleanup: ลบไฟล์ที่ไม่ใช้แล้ว ทั้ง LFS และ History
# ติดตั้ง Git LFS Server บน Ubuntu
sudo apt-get update
sudo apt-get install golang-go
# Clone Git LFS Server Repository
git clone https://github.com/git-lfs/lfs-test-server.git
cd lfs-test-server
go build
# สร้าง Directory สำหรับ LFS Storage
sudo mkdir -p /var/lfs-storage
sudo chown $USER:$USER /var/lfs-storage
# รัน LFS Server
./lfs-test-server --listen=0.0.0.0:3000 --root=/var/lfs-storage
สรุป
การจัดการไฟล์ขนาดใหญ่ใน Git อย่างถูกต้องนั้นสำคัญสำหรับ Repository ที่เสถียร โดยใช้ Git LFS หรือ Self-hosted LFS Server บน ผู้ให้บริการโฮสติ้ง Cloud VPS คุณสามารถจัดเก็บไฟล์ขนาดใหญ่ได้อย่างมีประสิทธิภาพ พร้อมควบคุมการเข้าถึงและ Backup อย่างเต็มที่
