bashadvanced

Git Sparse Checkout — Clone Only What You Need

Use sparse checkout to clone specific directories from large monorepos without downloading everything.

bash
# Clone with sparse checkout (no working tree initially)
git clone --filter=blob:none --sparse https://github.com/org/monorepo.git
cd monorepo

# Initialize sparse-checkout in cone mode (recommended)
git sparse-checkout init --cone

# Add directories you need
git sparse-checkout set packages/shared packages/frontend

# Add more directories later
git sparse-checkout add packages/backend

# View current sparse-checkout patterns
git sparse-checkout list
# packages/shared
# packages/frontend
# packages/backend

# Disable sparse checkout (restore full working tree)
git sparse-checkout disable

# --- Non-cone mode (pattern-based) ---
git sparse-checkout init --no-cone
git sparse-checkout set '!/*' '/README.md' '/packages/api/**'
# ! excludes everything first, then includes specific patterns

# Check what's checked out vs excluded
git ls-tree -r HEAD --name-only | head -20

# Partial clone + sparse = fastest monorepo experience
git clone --filter=blob:none --sparse --depth=1 \
  https://github.com/org/monorepo.git

Use Cases

  • Working on a specific package in a monorepo
  • Reducing CI clone times for large repositories
  • Contributing to specific subdirectories of OSS projects

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.