LinuxCommandLibrary

git-sparse-checkout

Checkout a subset of repository files

TLDR

Enable sparse checkout

$ git sparse-checkout init
copy

Disable sparse-checkout and restore full repository
$ git sparse-checkout disable
copy

Specify which directories (or files) to include
$ git sparse-checkout set [path/to/directory]
copy

Add more paths later
$ git sparse-checkout add [path/to/directory]
copy

SYNOPSIS

git sparse-checkout init [--cone]
git sparse-checkout set [--[no-]cone] [--skip-checks] <pattern>…
git sparse-checkout add [--[no-]cone] [--skip-checks] <pattern>…
git sparse-checkout remove <pathspec>…
git sparse-checkout list
git sparse-checkout reapply
git sparse-checkout disable

PARAMETERS

init
    Initialize .git/info/sparse-checkout file; enable cone mode with --cone.

set
    Replace sparse-checkout patterns with specified <pattern>s.

add
    Append <pattern>s to existing sparse-checkout patterns.

remove
    Remove specified <pathspec>s from sparse-checkout patterns.

list
    Print current sparse-checkout patterns to stdout.

reapply
    Update working tree to match current sparse-checkout patterns.

disable
    Disable sparse-checkout and restore full checkout.

--cone
    Activate cone mode (directory-based patterns).

--no-cone
    Use legacy non-cone (glob pattern) mode.

--skip-checks
    Skip path existence and validity checks.

DESCRIPTION

git sparse-checkout configures the sparse-checkout feature, enabling users to check out only a subset of a repository's files into the working directory. This is ideal for large repositories or monorepos, reducing disk usage, improving performance, and speeding up operations like checkout and status.

Sparse-checkout requires enabling core.sparseCheckout = true via git config. Patterns defining included paths are stored in .git/info/sparse-checkout. Two modes exist: traditional glob patterns or efficient 'cone' mode for directory hierarchies.

Key subcommands include init to set up the file, set or add to define patterns (e.g., /*, docs/), remove to exclude paths, list to view patterns, reapply to refresh the working tree after changes, and disable to turn off the feature. Cone mode simplifies patterns by including entire subdirectories recursively unless excluded.

After updating patterns, use git checkout or git read-tree -m -u HEAD to apply changes. This command streamlines workflows for developers focusing on specific project areas.

CAVEATS

Requires core.sparseCheckout = true. Cone mode patterns must start at repo root or use /*/, excludes need ! prefix. Untracked files outside patterns persist until cleaned.

CONE MODE PATTERNS

Includes entire directories recursively. Examples: teamA/ (all under teamA), /* (top-level only), !/docs/ (exclude docs).

EXAMPLE USAGE

git sparse-checkout init --cone
git sparse-checkout set src/ tests/
git checkout main

HISTORY

Introduced in Git 2.25.0 (January 2020) as a dedicated subcommand. Sparse-checkout feature dates to Git 1.7.7 (2011) via manual config, enhanced with cone mode in 2.19.

SEE ALSO

git config(1), git checkout(1), git read-tree(1)

Copied to clipboard