git-sparse-checkout - Linux
Overview
git-sparse-checkout enables selective partial clones of Git repositories. It lets you fetch and checkout only a specific subset of files and directories, reducing bandwidth and storage usage. This is particularly useful for large or sprawling repositories where you need to work on a specific module or section.
Syntax
git sparse-checkout [options...] [<tree-ish>]
git read-tree -s <tree-ish>
<tree-ish>
: A commit, branch, tag, or tree-ish to checkout. Defaults to the current commit.
Options/Flags
-f|--force
: Override an existing checkout.-q|--quiet
: Suppress progress messages.-l|--sparse-output
: List entries in the sparse index.-L|--sparse-checkout-log
: Print a git log of the sparse checkout.-C|--full
: Perform a full checkout instead of a sparse checkout.-P|--prune
: Remove untracked files and directories not in the sparse checkout.-t|--tree
: Use the provided tree-ish instead of the current commit.-u|--unsparse
: Unsparsify the working directory.-k|--sparse-file
<file>
: Specify a custom file for the sparse index. Defaults to.git/info/sparse-checkout
.
Examples
Simple Usage:
git sparse-checkout master --sparse-file=my-files
git sparse-checkout my-branch
Checkout Specific Directories:
git sparse-checkout master --sparse-file=my-files
echo directories/dir1 >> .git/info/sparse-checkout
echo directories/dir2 >> .git/info/sparse-checkout
git read-tree -s master
Checkout Specific Files:
git sparse-checkout master --sparse-file=my-files
echo libs/file1.c >> .git/info/sparse-checkout
echo libs/file2.h >> .git/info/sparse-checkout
git read-tree -s master
Common Issues
Untracked Files: If the --prune
option is not used, untracked files outside the sparse checkout may remain in the working directory.
Nested Sparse Checkouts: If a file or directory is already part of a sparse checkout and you add it to another sparse checkout, the later one will take precedence.
Conflicting Sparse Checkouts: If multiple sparse checkouts with conflicting settings are applied, the last applied setting will be used.
Integration
Shell Script Example:
#!/bin/bash
# Check if a file is in the sparse checkout
git sparse-checkout -l | grep "libs/file1.c"
# Add a file to the sparse checkout
echo "libs/file3.c" >> .git/info/sparse-checkout
git read-tree -s master
Related Commands
git fetch
: Fetch objects from a remote repository.- git clone`: Clone a repository into new location.
git diff
: Show differences between two files or commits.git add
: Add files to the staging area.git checkout
: Checkout a specific commit or branch.