Nâng CaoGit iconGit

Partial clone và shallow clone: khi nào dùng trong CI/CD để tăng tốc pipeline?

Full clone của large monorepo (Facebook-scale) có thể mất 30+ phút. CI/CD cần tối ưu.

Shallow clone — chỉ clone N commits gần nhất:

bash
git clone --depth 1 https://github.com/org/repo
# Chỉ lấy latest commit, không có history
# Tốt cho: CI build, Docker build layer

# Trong GitHub Actions:
- uses: actions/checkout@v4
  with:
    fetch-depth: 1  # shallow
    # fetch-depth: 0 = full history (cần cho semantic-release)

Partial clone — clone repo nhưng defer downloading large blobs:

bash
# Blobless clone: download trees nhưng defer blobs — phù hợp nhất cho CI
git clone --filter=blob:none https://github.com/org/repo
# Blobs tải về khi bạn thực sự checkout file đó (không phải "chỉ metadata")

# Treeless clone (filter=tree:0): không tải trees — checkout gần như không dùng được
# Chỉ dùng cho special cases (shallow inspection), không phải CI thông thường
git clone --filter=tree:0 https://github.com/org/repo

Sparse checkout — chỉ checkout 1 subdirectory của monorepo:

bash
git clone --no-checkout https://github.com/org/monorepo
cd monorepo
git sparse-checkout init --cone
git sparse-checkout set apps/my-app packages/shared
git checkout main

CI/CD best practice: --depth 1 + --filter=blob:none + sparse checkout = clone time giảm 90% cho large monorepos.

Lưu ý: shallow clone làm git bisectgit log --all không hoạt động đầy đủ.

Xem toàn bộ Git cùng filter theo level & chủ đề con.

Mở danh sách Git