今日目標
• 學習管理大型專案的策略
• 理解 Git Submodules 的使用
• 掌握 Monorepo 的概念
• 學會優化大型 repository 效能
為什麼需要特殊策略?
小專案 vs 大型專案:
小專案(個人作品集):
├── index.html
├── style.css
└── script.js
→ 3 個檔案,簡單管理
大型專案(企業系統):
├── frontend/(1000+ 檔案)
├── backend/(2000+ 檔案)
├── mobile-app/(800+ 檔案)
├── shared-components/(500+ 檔案)
└── documentation/(300+ 檔案)
→ 4000+ 檔案,需要策略!
職場真實挑戰:
❌ 沒有策略的問題:
✅ 有策略的解決:
操作步驟
步驟1:理解專案結構策略
策略1:Monorepo(單一倉庫)
優點:
✅ 所有程式碼在一個 repo
✅ 容易跨專案重構
✅ 版本管理一致
✅ 共享工具和設定
缺點:
❌ Repository 可能很大
❌ Clone 時間長
❌ 權限管理複雜
適用:
缺點:
❌ 跨專案修改困難
❌ 版本管理複雜
❌ 重複的設定檔
適用:
mkdir company-platform
cd company-platform
git init
mkdir docs
echo "# 公司平台專案" > README.md
git add .
git commit -m "init: 初始化專案"
git submodule add https://github.com/你的帳號/shared-utils.git libs/shared-utils
git submodule status
git add .
git commit -m "feat: 新增共用函式庫 submodule"
Submodule 基本操作:
git clone --recursive https://github.com/你的帳號/company-platform.git
git submodule init
git submodule update
git submodule update --init --recursive
cd libs/shared-utils
git pull origin main
cd ../..
git add libs/shared-utils
git commit -m "chore: 更新共用函式庫版本"
git submodule deinit libs/shared-utils
git rm libs/shared-utils
rm -rf .git/modules/libs/shared-utils
git commit -m "chore: 移除共用函式庫 submodule"
步驟3:使用 Sparse Checkout(稀疏檢出)
只下載需要的檔案:
git clone --no-checkout https://github.com/大型專案/repo.git
cd repo
git sparse-checkout init --cone
git sparse-checkout set frontend/components
git sparse-checkout set frontend/components backend/api
git sparse-checkout list
git checkout main
步驟4:使用 Shallow Clone(淺層複製)
只下載最近的歷史:
git clone --depth 1 https://github.com/大型專案/repo.git
git clone --depth 10 https://github.com/大型專案/repo.git
git clone --depth 1 --branch main https://github.com/大型專案/repo.git
git fetch --deepen=100 # 增加 100 個 commit 的深度
git fetch --unshallow
效果比較:
完整 clone:
時間:10 分鐘
大小:2 GB
歷史:10,000 commits
Shallow clone (depth=1):
時間:30 秒
大小:50 MB
歷史:1 commit
步驟5:大型專案的目錄結構
建議的結構:
mkdir -p company-platform/{apps,packages,tools,docs}
company-platform/
├── apps/ # 應用程式
│ ├── web-app/ # 網頁應用
│ ├── mobile-app/ # 行動應用
│ └── admin-panel/ # 管理後台
├── packages/ # 共用套件
│ ├── ui-components/ # UI 元件庫
│ ├── utils/ # 工具函數
│ └── api-client/ # API 客戶端
├── tools/ # 開發工具
│ ├── build-scripts/ # 建置腳本
│ └── linters/ # 程式碼檢查
├── docs/ # 文件
│ ├── architecture/ # 架構文件
│ ├── api/ # API 文件
│ └── guides/ # 開發指南
├── .github/ # GitHub 設定
│ ├── workflows/ # CI/CD
│ └── CODEOWNERS # 程式碼擁有者
└── README.md # 專案說明
建立 CODEOWNERS 檔案:
cat > .github/CODEOWNERS << 'EOF'
/apps/web-app/ @frontend-team
/packages/ui-components/ @frontend-team
/apps/api/ @backend-team
/packages/api-client/ @backend-team
/.github/workflows/ @devops-team
/tools/ @devops-team
/docs/ @tech-writers
EOF
步驟6:大型專案的 .gitignore
cat > .gitignore << 'EOF'
node_modules/
vendor/
*.pyc
pycache/
dist/
build/
*.egg-info/
*.min.js
*.min.css
.env
.env.local
*.local
.vscode/
.idea/
*.swp
*.swo
.DS_Store
Thumbs.db
*.log
*.sqlite
*.db
coverage/
.coverage
htmlcov/
*.tmp
*.temp
*.cache
EOF
大型專案管理技巧
技巧1:使用 Git LFS(大檔案儲存)
git lfs install
git lfs track ".psd"
git lfs track ".zip"
git lfs track ".mp4"
git lfs track "design/**/.ai"
git lfs track
git add .gitattributes
git commit -m "chore: 設定 Git LFS 追蹤大型檔案"
技巧2:定期清理歷史
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 |
tail -10
git filter-branch --tree-filter 'rm -f 大檔案.zip' HEAD
技巧3:使用 Git Worktree
git worktree add ../project-feature feature/new-function
git worktree list
cd ../project-feature
git worktree remove ../project-feature
技巧4:優化 Git 設定
git config core.fsmonitor true
git config core.untrackedcache true
git config core.compression 9
git config pack.windowMemory "100m"
git config pack.packSizeLimit "100m"
git config pack.threads "4"
git config submodule.fetchJobs 4
Monorepo 實戰範例
建立 Monorepo 結構:
mkdir company-monorepo
cd company-monorepo
git init
mkdir -p {apps,packages}/{web,mobile,shared}
cat > package.json << 'EOF'
{
"name": "company-monorepo",
"private": true,
"workspaces": [
"apps/",
"packages/"
],
"scripts": {
"dev": "npm run dev --workspaces",
"build": "npm run build --workspaces",
"test": "npm run test --workspaces"
}
}
EOF
cat > README.md << 'EOF'
apps/
- 應用程式packages/
- 共用套件npm install # 安裝所有依賴
npm run dev # 啟動開發伺服器
npm run build # 建置所有專案
npm run test # 執行所有測試
EOF
提交初始結構
git add . git commit -m "init: 建立 monorepo 基礎結構"
---
## **最佳實踐總結**
### **✅ 大型專案管理清單:**
專案結構: □ 清楚的目錄組織 □ 設定 CODEOWNERS □ 完整的 .gitignore □ 詳細的 README
效能優化: □ 使用 shallow clone □ 啟用 sparse checkout □ 設定 Git LFS □ 定期清理歷史
團隊協作: □ 明確的責任劃分 □ 獨立的 CI/CD 流程 □ 版本管理策略 □ Code Review 規範
工具使用: □ Submodules 或 Monorepo □ 自動化測試 □ 文件自動生成 □ 效能監控
---
## **今日重點回顧**
- ✅ 理解大型專案的管理挑戰
- ✅ 學會使用 Git Submodules
- ✅ 掌握 Sparse Checkout 和 Shallow Clone
- ✅ 了解 Monorepo vs Multi-repo 策略
## **核心指令總結**
```bash
# Submodules
git submodule add <url> # 新增 submodule
git submodule update --init # 初始化 submodule
git clone --recursive # 複製包含 submodules
# 效能優化
git clone --depth 1 # 淺層複製
git sparse-checkout set <dir> # 稀疏檢出
git lfs track "*.zip" # 追蹤大檔案
# Worktree
git worktree add <path> <branch> # 建立工作目錄
git worktree list # 列出所有 worktree