Sparse checkout in Git is a feature that allows you to selectively check out specific files or directories within a repository. This is particularly useful when working with large repositories, enabling you to work with only the relevant files without downloading the entire codebase. By configuring sparse checkout, you can optimize disk space, improve performance, and keep a focused workspace.
Sparse checkout allows developers to download only specific parts of a Git repository. This feature is ideal when:
To use sparse checkout in Git, follow these steps:
Run the following command in the terminal to enable sparse checkout
git config core.sparseCheckout true
This command sets Git to allow sparse checkouts by updating the repository’s configuration.
After enabling sparse checkout mode, initialize the sparse checkout setup
git sparse-checkout init
This command creates a sparse-checkout configuration file located at .git/info/sparse-checkout
, where you will define the specific files or directories to check out.
If you haven’t cloned the repository yet, clone it with
git clone
This step downloads the repository without triggering the sparse checkout setup, which you can configure in the next steps.
Once sparse checkout mode is activated, you need to specify the directories or files you want to include in your working directory. There are two ways to do this:
.git/info/sparse-checkout
.git/info/sparse-checkout
file in a text editor.
/src/
/docs/
Here, only the src
and docs
directories are checked out. All other files and directories are ignored.
git sparse-checkout
CommandInstead of manually editing the file, you can use Git commands to define paths.
git sparse-checkout set src/ docs/
src/
and docs/
.
project/
├── src/
│ ├── main.c
│ └── util.c
├── docs/
│ └── guide.md
├── tests/
│ └── test.c
If you add only /src/
and /docs/
to the sparse checkout file, only these folders will be available in your local copy.
Git provides multiple options for adjusting sparse paths over time.
add
:If new folders or files are needed, add them without removing the existing configuration:
git sparse-checkout add assets/
To view which paths are currently included in your sparse checkout configuration:
git sparse-checkout list
This lists all paths that are currently being checked out in the sparse configuration.
set
:If you want to redefine paths entirely, use
git sparse-checkout set newpath/
This command will replace all previous paths with newpath/
.
Git’s sparse checkout configuration supports patterns, allowing complex configurations. You can include or exclude files based on patterns, making sparse checkout versatile.
For example, you can include only specific file types or structures:
/docs/*.md
/src/*.c
This configuration checks out only markdown files in docs
and C files in src
.
You can exclude paths by prefixing them with !
. For instance, to exclude the tests
folder
!tests/
Wildcards such as *
, ?
, and character ranges [ ]
enable complex path definitions.
*.js
: Includes all JavaScript files.
docs/*/
: Includes all subdirectories within docs
.
Sparse checkout can be useful in several scenarios, such as:
.git/info/sparse-checkout
file concise, listing only essential directories to keep storage usage low.Git’s sparse checkout feature is a practical solution for managing large repositories by allowing selective checkout of files and directories. With a range of commands and pattern-based options, sparse checkout lets you configure your workspace for optimal performance, storage management, and focus. Happy coding !❤️