Skip to content

sparse-checkout Git Command Guide

The git sparse-checkout command enables working with only a subset of files from a repository, allowing efficient handling of large repositories by checking out only the necessary directories and files. This is particularly useful for monorepos, large projects, and focused development workflows.

Terminal window
git sparse-checkout <subcommand> [<options>] [<patterns>...]
SubcommandDescription
initInitialize sparse-checkout configuration
listList currently active patterns
setSet the active patterns
addAdd patterns to the active set
disableDisable sparse-checkout
reapplyReapply sparse-checkout patterns
OptionDescription
--coneUse cone mode (directory-based patterns)
--no-coneUse non-cone mode (file-based patterns)
--sparse-indexUse sparse index (experimental)
--no-sparse-indexDon’t use sparse index
ParameterDescription
<patterns>Path patterns to include/exclude
Traditional Full Checkout:
├── Repository: All files checked out
├── Working Tree: Complete directory structure
├── Performance: Slower for large repos
└── Storage: Full repository size
Sparse Checkout:
├── Repository: Selective file checkout
├── Working Tree: Only specified paths
├── Performance: Faster, reduced I/O
└── Storage: Minimal working tree size
Cone Mode (Directory-Based):
├── Patterns: /*/ (directory patterns)
├── Efficient: Git can optimize queries
├── Simple: Easy to understand
└── Recommended: For most use cases
Non-Cone Mode (File-Based):
├── Patterns: Any glob patterns
├── Flexible: Fine-grained control
├── Complex: Harder to optimize
└── Advanced: For specific needs
Configuration States:
├── Disabled: Normal full checkout
├── Enabled: Sparse patterns active
├── Cone Mode: Directory-based patterns
├── Non-Cone Mode: File-based patterns
└── Reapplied: Patterns re-evaluated
Terminal window
# Initialize sparse checkout (enables it)
git sparse-checkout init
# Initialize with cone mode (recommended)
git sparse-checkout init --cone
# Initialize without cone mode
git sparse-checkout init --no-cone
Terminal window
# Set specific directories (cone mode)
git sparse-checkout set src/ docs/ tests/
# Set file patterns (non-cone mode)
git sparse-checkout set --no-cone 'src/**/*.c' 'include/**/*.h'
# Add patterns to existing set
git sparse-checkout add scripts/ tools/
Terminal window
# List current patterns
git sparse-checkout list
# Reapply current patterns
git sparse-checkout reapply
# Disable sparse checkout
git sparse-checkout disable
Terminal window
# Focus on specific services in monorepo
git sparse-checkout set services/auth/ services/api/ shared/libs/
# Work on frontend components
git sparse-checkout set frontend/components/ frontend/styles/
# Switch between different parts
git sparse-checkout set backend/ infrastructure/
Terminal window
# Clone with sparse checkout
git clone --filter=blob:none --sparse <repo-url>
cd <repo>
git sparse-checkout set <desired-paths>
# Optimize for CI/CD
git sparse-checkout set src/ tests/ --cone
Terminal window
# Different checkouts per branch
git checkout feature/frontend
git sparse-checkout set frontend/
git checkout feature/backend
git sparse-checkout set backend/ shared/
# Reset to full checkout on main
git checkout main
git sparse-checkout disable
Terminal window
# Configure sparse checkout behavior
git config core.sparseCheckout true # Enable sparse checkout
git config core.sparseCheckoutCone true # Use cone mode by default
git config index.sparse true # Use sparse index
# Configure clone behavior
git config clone.filter blob:none # Clone without blobs initially
git config clone.sparse true # Enable sparse checkout on clone
Terminal window
# Use cone mode for directory-based work
git sparse-checkout init --cone
git sparse-checkout set src/ tests/
# Keep patterns simple and organized
git sparse-checkout set services/user/ services/auth/ shared/utils/
# Use disable to temporarily work with full repo
git sparse-checkout disable
# ... work with full repository ...
git sparse-checkout reapply
Terminal window
# Check current status
git sparse-checkout list
# Verify patterns before applying
git sparse-checkout set --dry-run src/ docs/
# Backup before major changes
cp .git/info/sparse-checkout .git/info/sparse-checkout.backup
#!/bin/bash
# Team sparse checkout workflow
setup_team_workspace() {
local team="$1"
local project="$2"
echo "Setting up workspace for team: $team, project: $project"
# Initialize sparse checkout
git sparse-checkout init --cone
# Set team-specific patterns
case "$team" in
frontend)
git sparse-checkout set \
"frontend/$project/" \
"shared/components/" \
"shared/styles/"
;;
backend)
git sparse-checkout set \
"backend/$project/" \
"shared/libs/" \
"infrastructure/"
;;
qa)
git sparse-checkout set \
"tests/" \
"scripts/ci/" \
"docs/testing/"
;;
esac
echo "Workspace configured for $team team"
}
# Usage
setup_team_workspace "frontend" "dashboard"
Terminal window
# CI/CD sparse checkout for faster builds
ci_sparse_setup() {
echo "Setting up sparse checkout for CI"
# Enable sparse checkout
git sparse-checkout init --cone
# Set minimal required paths
case "$CI_JOB_TYPE" in
test)
git sparse-checkout set src/ tests/ scripts/ci/
;;
build)
git sparse-checkout set src/ assets/ build/
;;
docs)
git sparse-checkout set docs/ scripts/docs/
;;
*)
# Full checkout for other jobs
git sparse-checkout disable
;;
esac
echo "CI sparse checkout configured"
}
ci_sparse_setup
Terminal window
# Manage different development environments
dev_environment() {
local env="$1"
echo "Setting up development environment: $env"
git sparse-checkout init --cone
case "$env" in
minimal)
git sparse-checkout set src/ README.md
;;
standard)
git sparse-checkout set src/ tests/ docs/
;;
full)
git sparse-checkout disable
;;
mobile)
git sparse-checkout set mobile/ shared/
;;
web)
git sparse-checkout set web/ shared/ assets/
;;
esac
echo "Environment '$env' configured"
}
# Usage
dev_environment "mobile"
Terminal window
# Check if sparse checkout is enabled
git config core.sparseCheckout
# Verify patterns file exists
cat .git/info/sparse-checkout
# Reinitialize if needed
git sparse-checkout init --cone
git sparse-checkout set <patterns>
Terminal window
# Files not showing up
git sparse-checkout list
# Add missing patterns
git sparse-checkout add missing/path/
# Reapply patterns
git sparse-checkout reapply
Terminal window
# Speed up sparse operations
git config core.sparseCheckoutCone true
# Use sparse index
git config index.sparse true
# Optimize for large repos
git sparse-checkout set --cone <minimal-paths>
Terminal window
# Patterns not applied after branch switch
git sparse-checkout reapply
# Different patterns per branch
git checkout feature-branch
git sparse-checkout set feature-specific/
Terminal window
# Check current mode
git config core.sparseCheckoutCone
# Convert between modes
git sparse-checkout init --cone # Switch to cone mode
git sparse-checkout init --no-cone # Switch to non-cone mode
Terminal window
# Clean up after sparse checkout issues
git sparse-checkout disable
git reset --hard HEAD
git clean -fd
# Reinitialize repository
rm -rf .git/info/sparse-checkout
git sparse-checkout init
#!/bin/bash
# Monorepo sparse checkout management
monorepo_setup() {
echo "=== Monorepo Sparse Checkout Setup ==="
# Initialize for large monorepo
git sparse-checkout init --cone
# Define service boundaries
services=(
"user-service"
"payment-service"
"notification-service"
"api-gateway"
)
# Setup service-specific checkout
setup_service() {
local service="$1"
echo "Setting up sparse checkout for service: $service"
# Core service files
git sparse-checkout set \
"services/$service/" \
"shared/libs/" \
"shared/protocols/" \
"infrastructure/docker/$service/" \
"scripts/deploy/$service/"
# Service-specific tests
git sparse-checkout add "tests/$service/"
echo "Service $service checkout configured"
}
# Cross-service development
setup_cross_service() {
echo "Setting up cross-service development"
git sparse-checkout set \
"services/" \
"shared/" \
"infrastructure/" \
"tests/integration/"
echo "Cross-service development configured"
}
# Interactive service selection
echo "Available services:"
for i in "${!services[@]}"; do
echo "$((i+1)). ${services[i]}"
done
echo "0. Cross-service development"
read -p "Select service (1-${#services[@]}, or 0): " choice
if [ "$choice" -eq 0 ]; then
setup_cross_service
elif [ "$choice" -ge 1 ] && [ "$choice" -le "${#services[@]}" ]; then
setup_service "${services[$((choice-1))]}"
else
echo "Invalid choice"
return 1
fi
}
monorepo_setup
Terminal window
# Platform-specific sparse checkout
platform_development() {
echo "=== Multi-Platform Development ==="
platforms=(
"web:frontend web/shared assets/"
"mobile:mobile native/shared assets/mobile/"
"desktop:desktop native/shared assets/desktop/"
"api:backend shared/libs infrastructure/"
)
# Setup platform development
setup_platform() {
local platform="$1"
local paths="$2"
echo "Setting up development for platform: $platform"
git sparse-checkout init --cone
# Add platform-specific paths
git sparse-checkout set $paths
# Add common development files
git sparse-checkout add \
"scripts/" \
"docs/development/" \
"tests/shared/"
echo "Platform $platform development environment ready"
}
# Show platform options
echo "Available platforms:"
for i in "${!platforms[@]}"; do
platform_name=$(echo "${platforms[i]}" | cut -d: -f1)
echo "$((i+1)). $platform_name"
done
read -p "Select platform (1-${#platforms[@]}): " choice
if [ "$choice" -ge 1 ] && [ "$choice" -le "${#platforms[@]}" ]; then
platform_data="${platforms[$((choice-1))]}"
platform_name=$(echo "$platform_data" | cut -d: -f1)
platform_paths=$(echo "$platform_data" | cut -d: -f2)
setup_platform "$platform_name" "$platform_paths"
else
echo "Invalid choice"
return 1
fi
}
platform_development
Terminal window
# Feature-specific sparse checkout
feature_development() {
echo "=== Feature Development Isolation ==="
# Get current feature branch
current_branch=$(git branch --show-current)
if [[ "$current_branch" == feature/* ]]; then
feature_name=$(echo "$current_branch" | sed 's/feature\///')
echo "Setting up sparse checkout for feature: $feature_name"
# Initialize sparse checkout
git sparse-checkout init --cone
# Set feature-specific paths
git sparse-checkout set \
"features/$feature_name/" \
"src/" \
"tests/features/$feature_name/" \
"shared/" \
"scripts/"
echo "Feature development environment configured"
echo "Working on: $feature_name"
echo "Active paths:"
git sparse-checkout list
else
echo "Not on a feature branch (current: $current_branch)"
echo "Switch to a feature branch first:"
echo "git checkout -b feature/your-feature-name"
return 1
fi
}
feature_development
Terminal window
# Optimize repository working tree size
repository_optimization() {
echo "=== Repository Size Optimization ==="
# Analyze repository structure
echo "Repository structure analysis:"
echo "Total directories: $(find . -type d | wc -l)"
echo "Total files: $(find . -type f | wc -l)"
echo "Working tree size: $(du -sh . | cut -f1)"
# Identify large directories
echo ""
echo "Largest directories:"
du -sh */ 2>/dev/null | sort -hr | head -10
# Setup optimized sparse checkout
echo ""
echo "Setting up optimized sparse checkout..."
# Common optimization patterns
optimization_profiles() {
echo "Optimization profiles:"
echo "1. Minimal (src + docs)"
echo "2. Development (src + tests + docs)"
echo "3. CI/CD (src + tests + scripts)"
echo "4. Documentation (docs + examples)"
echo "5. Custom"
read -p "Select profile (1-5): " profile
git sparse-checkout init --cone
case "$profile" in
1) git sparse-checkout set src/ docs/ ;;
2) git sparse-checkout set src/ tests/ docs/ ;;
3) git sparse-checkout set src/ tests/ scripts/ ;;
4) git sparse-checkout set docs/ examples/ ;;
5)
echo "Enter custom paths (space-separated):"
read -r custom_paths
git sparse-checkout set $custom_paths
;;
*) echo "Invalid profile"; return 1 ;;
esac
echo "Optimization applied"
echo "Working tree size reduced to: $(du -sh . | cut -f1)"
}
optimization_profiles
}
repository_optimization
Terminal window
# Collaborative sparse checkout patterns
collaborative_patterns() {
echo "=== Collaborative Development Patterns ==="
# Team-based sparse checkout
team_setup() {
local team="$1"
echo "Setting up team-based sparse checkout for: $team"
git sparse-checkout init --cone
case "$team" in
frontend)
git sparse-checkout set \
"frontend/" \
"shared/components/" \
"shared/styles/" \
"assets/" \
"docs/frontend/"
;;
backend)
git sparse-checkout set \
"backend/" \
"shared/libs/" \
"infrastructure/" \
"scripts/deploy/" \
"docs/backend/"
;;
devops)
git sparse-checkout set \
"infrastructure/" \
"scripts/ci/" \
"scripts/deploy/" \
"docker/" \
"docs/devops/"
;;
qa)
git sparse-checkout set \
"tests/" \
"scripts/test/" \
"docs/testing/" \
"shared/test-utils/"
;;
esac
echo "Team setup complete for $team"
}
# Project-based checkout
project_setup() {
local project="$1"
echo "Setting up project-based sparse checkout for: $project"
git sparse-checkout init --cone
# Find all paths related to project
project_paths=$(find . -type d -name "*$project*" 2>/dev/null | head -10)
if [ -n "$project_paths" ]; then
git sparse-checkout set $project_paths
echo "Project paths configured"
else
echo "No project-specific paths found, using minimal setup"
git sparse-checkout set "src/" "tests/" "docs/"
fi
}
# Role-based access
role_based_setup() {
local role="$1"
echo "Setting up role-based sparse checkout for: $role"
git sparse-checkout init --cone
case "$role" in
developer)
git sparse-checkout set src/ tests/ docs/ scripts/
;;
reviewer)
git sparse-checkout set src/ tests/ docs/
;;
maintainer)
git sparse-checkout set . # Full checkout
;;
contributor)
git sparse-checkout set src/ tests/ docs/ examples/
;;
esac
echo "Role-based setup complete for $role"
}
# Interactive setup
echo "Collaborative setup options:"
echo "1. Team-based setup"
echo "2. Project-based setup"
echo "3. Role-based setup"
read -p "Select setup type (1-3): " setup_type
case "$setup_type" in
1)
read -p "Enter team (frontend/backend/devops/qa): " team
team_setup "$team"
;;
2)
read -p "Enter project name: " project
project_setup "$project"
;;
3)
read -p "Enter role (developer/reviewer/maintainer/contributor): " role
role_based_setup "$role"
;;
*)
echo "Invalid option"
return 1
;;
esac
}
collaborative_patterns

What’s the difference between git sparse-checkout and partial clones?

Section titled “What’s the difference between git sparse-checkout and partial clones?”

Sparse checkout controls which files are checked out to working directory, while partial clones (—filter) control which objects are downloaded from server. They can be used together.

How do I enable sparse checkout on an existing repository?

Section titled “How do I enable sparse checkout on an existing repository?”

Run git sparse-checkout init to enable it, then git sparse-checkout set to define which paths to include.

Can I use sparse checkout with existing working directory?

Section titled “Can I use sparse checkout with existing working directory?”

Yes, but you may need to git sparse-checkout set and then git sparse-checkout reapply to update the working directory.

What’s the difference between cone and non-cone mode?

Section titled “What’s the difference between cone and non-cone mode?”

Cone mode uses directory-based patterns (more efficient), non-cone mode allows arbitrary file patterns (more flexible but slower).

How do I temporarily disable sparse checkout?

Section titled “How do I temporarily disable sparse checkout?”

Use git sparse-checkout disable to temporarily work with the full repository, then git sparse-checkout reapply to restore patterns.

Can sparse checkout work with git submodules?

Section titled “Can sparse checkout work with git submodules?”

Yes, sparse checkout can be combined with submodules, but each repository manages its own sparse checkout settings.

How do I see which patterns are currently active?

Section titled “How do I see which patterns are currently active?”

Use git sparse-checkout list to see the currently active patterns.

Can I add patterns to existing sparse checkout?

Section titled “Can I add patterns to existing sparse checkout?”

Yes, use git sparse-checkout add to add new patterns to the existing set.

How do I change from cone to non-cone mode?

Section titled “How do I change from cone to non-cone mode?”

Disable sparse checkout, then re-initialize with the desired mode: git sparse-checkout disable && git sparse-checkout init —no-cone

What’s the performance impact of sparse checkout?

Section titled “What’s the performance impact of sparse checkout?”

Sparse checkout can significantly improve performance for large repositories by reducing working tree size and Git operations scope.

Can sparse checkout work with git status and other commands?

Section titled “Can sparse checkout work with git status and other commands?”

Yes, all Git commands work normally with sparse checkout - they just operate on the subset of files currently checked out.

How do I clone a repository with sparse checkout from the start?

Section titled “How do I clone a repository with sparse checkout from the start?”

Use git clone —sparse , then cd into the repository and set your desired patterns.

Can I use sparse checkout with remote branches?

Section titled “Can I use sparse checkout with remote branches?”

Yes, sparse checkout patterns apply to all branches. Switch branches normally after setting up sparse checkout.

How do I troubleshoot sparse checkout issues?

Section titled “How do I troubleshoot sparse checkout issues?”

Check git sparse-checkout list, verify .git/info/sparse-checkout file, and try git sparse-checkout reapply.

Can sparse checkout work with git clean and git reset?

Section titled “Can sparse checkout work with git clean and git reset?”

Yes, these commands work normally but only affect the files currently in your sparse checkout.

How do I migrate from full checkout to sparse checkout?

Section titled “How do I migrate from full checkout to sparse checkout?”

Run git sparse-checkout init —cone, then git sparse-checkout set , then git sparse-checkout reapply.

—sparse-index enables experimental sparse index feature that can improve performance with sparse checkouts by making the index sparse as well.

Can I use wildcards in sparse checkout patterns?

Section titled “Can I use wildcards in sparse checkout patterns?”

In non-cone mode, yes. In cone mode, patterns are directory-based with implicit /*/ matching.

How do I check if sparse checkout is enabled?

Section titled “How do I check if sparse checkout is enabled?”

Check git config core.sparseCheckout or look for .git/info/sparse-checkout file.

Can sparse checkout patterns include negated patterns?

Section titled “Can sparse checkout patterns include negated patterns?”

No, sparse checkout only supports positive patterns (include these paths). Use .gitignore for exclusion.

Applications of the git sparse-checkout command

Section titled “Applications of the git sparse-checkout command”
  1. Large Repository Management: Efficiently work with monorepos and large codebases
  2. Team Development: Enable focused development on specific project areas
  3. CI/CD Optimization: Speed up builds by checking out only necessary files
  4. Platform-Specific Development: Work on platform-specific code in multi-platform projects
  5. Feature Isolation: Develop features with minimal repository footprint
  6. Performance Optimization: Reduce disk usage and improve operation speed
  7. Collaborative Workflows: Support different team working patterns
  8. Development Environment Management: Customize working environments per developer/role