今日目標
• 學習診斷 Git 效能問題
• 掌握加速 Git 操作的技巧
• 優化 repository 體積和速度
• 建立高效的 Git 工作環境
為什麼需要效能優化?
職場真實情況:
❌ 效能差的體驗:
git status → 等 30 秒...
git log → 等 20 秒...
git diff → 等 40 秒...
git clone → 等 30 分鐘...
→ 😫 工作效率極差
✅ 優化後的體驗:
git status → 0.5 秒
git log → 1 秒
git diff → 2 秒
git clone → 2 分鐘
→ 😊 工作順暢
常見效能問題:
• 🐌 Repository 太大:歷史檔案太多
• 📁 工作目錄龐大:檔案數量過多
• 🗄️ 物件庫肥大:未優化的儲存
• 🔄 網路速度慢:下載上傳緩慢
操作步驟
步驟1:診斷效能問題
檢查 repository 大小:
cd company-website
git count-objects -vH
找出大型物件:
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
sed -n 's/^blob //p' |
sort --numeric-sort --key=2 --reverse |
head -10
測量操作時間:
time git status
time git log --oneline -100
time git diff HEAD~10
步驟2:優化 Git 設定
基本效能設定:
git config core.fsmonitor true
git config core.untrackedcache true
git config pack.threads 4
git config core.compression 9
git config submodule.fetchJobs 4
git config core.packedGitLimit 512m
git config core.packedGitWindowSize 512m
git config pack.deltaCacheSize 2047m
git config pack.packSizeLimit 2047m
git config pack.windowMemory 2047m
查看目前設定:
git config --list
git config core.fsmonitor
git config core.untrackedcache
步驟3:清理和維護 Repository
執行 Git 垃圾回收:
git gc
git gc --aggressive
git gc --auto
git fsck --full
移除未使用的物件:
git prune
git prune --expire=7.days.ago
git reflog expire --expire=now --all
git gc --prune=now --aggressive
壓縮 repository:
git repack -a -d
git repack -a -d --depth=250 --window=250
步驟4:減少 Repository 體積
移除大型檔案歷史:
cp -r .git .git.backup
git rev-list --objects --all |
git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' |
awk '/^blob/ {print substr($0,6)}' |
sort --numeric-sort --key=2 --reverse |
head -20
git filter-branch --force --index-filter
'git rm --cached --ignore-unmatch 大檔案.zip'
--prune-empty --tag-name-filter cat -- --all
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now --aggressive
使用 BFG Repo-Cleaner(更快更安全):
java -jar bfg.jar --strip-blobs-bigger-than 10M
java -jar bfg.jar --delete-files 檔案名稱.zip
java -jar bfg.jar --delete-folders 資料夾名稱
git reflog expire --expire=now --all
git gc --prune=now --aggressive
步驟5:使用 Shallow Clone
快速下載專案:
git clone --depth 1 https://github.com/使用者/專案.git
git clone --depth 10 https://github.com/使用者/專案.git
git clone --depth 1 --branch main --single-branch https://github.com/使用者/專案.git
效能比較:
完整 clone:
時間:15 分鐘
大小:2.5 GB
歷史:50,000 commits
Shallow clone (--depth 1):
時間:45 秒
大小:80 MB
歷史:1 commit
速度提升:20 倍!
需要更多歷史時:
git fetch --deepen=100
git fetch --unshallow
步驟6:優化大型工作目錄
使用 .gitignore 排除不必要的檔案:
cat > .gitignore << 'EOF'
node_modules/
vendor/
bower_components/
dist/
build/
*.min.js
*.min.css
*.log
logs/
*.tmp
*.temp
.cache/
.DS_Store
Thumbs.db
.vscode/
.idea/
*.swp
coverage/
.nyc_output/
EOF
移除已追蹤但應忽略的檔案:
git rm --cached -r node_modules/
git commit -m "chore: 停止追蹤 node_modules"
git rm --cached 檔案名稱
git commit -m "chore: 停止追蹤不需要的檔案"
步驟7:使用 Git LFS(大型檔案儲存)
設定 Git LFS:
git lfs install
git lfs track ".psd"
git lfs track ".zip"
git lfs track ".mp4"
git lfs track ".pdf"
git lfs track "design/**"
git lfs track
git add .gitattributes
git commit -m "chore: 設定 Git LFS"
遷移現有大檔案到 LFS:
git lfs migrate import --include="*.zip"
git lfs migrate import --include="design/*"
git lfs ls-files
git lfs du
進階優化技巧
技巧1:使用 Partial Clone
git clone --filter=blob:none https://github.com/使用者/專案.git
git clone --filter=blob:limit=1m https://github.com/使用者/專案.git
git clone --filter=tree:0 https://github.com/使用者/專案.git
技巧2:使用 Git Worktree 避免重複 Clone
git worktree add ../project-feature feature/new-function
git worktree add ../project-hotfix hotfix/urgent-fix
技巧3:定期維護腳本
cat > git-maintenance.sh << 'EOF'
#!/bin/bash
echo "🔧 開始 Git 維護..."
echo "📦 清理未引用物件..."
git prune --expire=30.days.ago
echo "🗑️ 執行垃圾回收..."
git gc --auto
echo "🔍 檢查完整性..."
git fsck --full --no-progress
echo "📦 優化儲存..."
git repack -a -d
echo "✅ 維護完成!"
echo ""
echo "📊 Repository 狀態:"
git count-objects -vH
EOF
chmod +x git-maintenance.sh
./git-maintenance.sh
技巧4:優化網路速度
git remote set-url origin git@github.com:使用者/專案.git
git config http.postBuffer 524288000 # 500MB
git config core.compression 9
git config pack.threads 4
效能優化檢查清單
✅ Repository 層級:
□ 執行 git gc 定期清理
□ 移除不需要的大檔案
□ 使用 Git LFS 管理大檔案
□ 定期執行 git repack
□ 檢查 .gitignore 是否完整
✅ 設定層級:
□ 啟用 core.fsmonitor
□ 啟用 core.untrackedcache
□ 設定適當的 pack.threads
□ 增加 core.compression
□ 優化網路相關設定
✅ 工作流程層級:
□ 使用 shallow clone 加速下載
□ 使用 sparse checkout 只下載需要的部分
□ 使用 git worktree 避免重複 clone
□ 定期執行維護腳本
□ 避免追蹤不必要的檔案
效能基準測試
優化前 vs 優化後:
操作 優化前 優化後 改善
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
git status 30 秒 0.5 秒 60倍
git log 20 秒 1 秒 20倍
git diff 40 秒 2 秒 20倍
git clone 30 分鐘 2 分鐘 15倍
Repository 2.5 GB 500 MB 5倍縮小
故障排除
問題1:git status 很慢
git config core.fsmonitor true
git config core.untrackedcache true
問題2:git clone 很慢
git clone --depth 1 --single-branch
問題3:repository 太大
git rev-list --objects --all |
git cat-file --batch-check |
sort -k 3 -n | tail -20
java -jar bfg.jar --strip-blobs-bigger-than 10M
git gc --aggressive --prune=now
問題4:push/pull 很慢
git config http.postBuffer 524288000
git config core.compression 9
今日重點回顧
• ✅ 學會診斷 Git 效能問題
• ✅ 掌握優化 Git 設定的技巧
• ✅ 會清理和維護 repository
• ✅ 能大幅提升 Git 操作速度
核心指令總結
git count-objects -vH # 查看大小
time git status # 測試速度
git config core.fsmonitor true # 加速 status
git config core.untrackedcache true # 快取未追蹤檔案
git gc --aggressive # 垃圾回收
git prune # 移除未用物件
git repack -a -d # 重新打包
git clone --depth 1 # 淺層複製
git clone --filter=blob:none # 部分複製