What is sparse checkout in Git?

Git is a versatile, free, and open-source version control system that helps keep track of changes made to project files. It’s like a helpful system for organizing and managing project work, making it easier for teams and individuals to work together smoothly. With Git, people can carefully watch for changes to project files, making sure everyone knows what’s happening and making it easier to work together on software projects.

However, sometimes, using Git can become difficult, especially with large repositories or monorepos. We will have to check out the entire repository, which is time-consuming and inefficient. This is especially problematic when you only need a small part, as it leads to slower performance and increased storage usage.

Sparse checkout

Sparse checkout is a feature in Git that allows us to checkout only a subset of files or directories from a repository, rather than the entire contents. This can be useful in situations where we have a large repository with many files, but we only need to work with or view a specific subset of files.

Before and after applying the sparse checkout
Before and after applying the sparse checkout

Key usages

Sparse checkout is really useful in a few key situations:

  • Large repositories: If we are working with repositories having large files or a ton of files, checking out just the parts you need can make things run faster.

  • Monorepos: In repositories that contain multiple projects, we can use the sparse checkout feature to work on only one specific project.

  • Selective work: Sometimes, we only need to make changes in a specific directory or file. Sparse checkout lets us focus on just that part.

Essential steps for executing sparse checkout

Here are the basic steps to perform a sparse checkout in Git:

  • First, we create a project directory MyProject and navigate into it using the following commands:

mkdir MyProject
cd MyProject
git init
  • Now, we create two files (file1.txt and file2.txt) and commit them:

touch file1.txt
git add file1.txt
git commit -m "My commit describing the file1.txt"
touch file2.txt
git add file2.txt
git commit -m "My commit describing the file2.txt"
Commands to commit the file1 and file2 changes
  • Now, we create a folder MyFolder, navigate into it, create and commit file3.txt within the folder and then navigate back one level using the cd .. command:

mkdir MyFolder
cd MyFolder
touch file3.txt
git add file3.txt
git commit -m "My commit describing the file3.txt"
cd ..
Commands to commit the directory and file3 changes
  • Now, we confirm the contents of the current directory using the ls command:

ls
  • To enable sparse checkout, we first initialize it in our repository:

git config core.sparseCheckout true
  • Then, we can define which files or directories we want to include in our sparse checkout by specifying their paths in a file named .git/info/sparse-checkout. For example:

echo "file1.txt" >> .git/info/sparse-checkout
echo "MyFolder/*" >> .git/info/sparse-checkout
Commands to sparse checkout file1 and MyFolder directory
  • After setting up the sparse checkout, we can update our working directory to include only the specified files and directories:

git read-tree -mu HEAD

This command reads the tree information from the current commit (HEAD) and updates the working directory accordingly. This ensures that only the files and directories specified in the sparse checkout configuration are included.

  • Finally, check out the files specified in the sparse-checkout file:

git checkout

After performing these steps, Git will only populate our working directory with the files and directories specified in the sparse-checkout configuration, rather than the entire repository.

Sparse checkout in action

Kindly click "Click to Connect." We have run all the commands for you in the terminal below. Please observe the output.

Terminal 1
Terminal
Loading...

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved