Git Sparse Checkout

Updated · 8 min read
hardGit 2.53gitsparse-checkoutmonorepoperformanceinterview

Sparse checkout is a Git feature that fills your working folder with only some of the repository's folders, while the repository itself still has every file. It is used in large repositories, often called monorepos, where many teams keep many projects in one repository and each person works on only a few of them. Fewer files on disk means faster git status, faster checkouts and an editor that isn't overwhelmed.

It is like a large office building where your key card opens only the floors you work on. The other floors are still there, and you can ask for access any time.

Syntax

Terminal
git sparse-checkout set <folder>...
git sparse-checkout add <folder>...
git sparse-checkout list
git sparse-checkout disable
CommandDescription
git sparse-checkout set <folder>...It is used to turn sparse checkout on and keep only the named folders (plus the files at the top level).
git sparse-checkout add <folder>...It is used to add more folders to the current selection.
git sparse-checkout listIt is used to show the selected folders.
git sparse-checkout reapplyIt is used to bring the working folder back in line with the selection.
git sparse-checkout disableIt is used to turn sparse checkout off and bring back every file.
git clone --sparse <url>It is used to clone with only the top-level files checked out.

The Starting Point

The Fee Tracker has grown into a monorepo: a Java back end, a web front end, a mobile app and the documentation, each in its own folder:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
mkdir -p backend/src mobile/android mobile/ios web/pages docs
echo "class FeeCalculator {}" > backend/src/FeeCalculator.java
echo "class HostelFees {}" > backend/src/HostelFees.java
echo "<h1>Fees</h1>" > web/pages/fees.html
echo "Android app" > mobile/android/README.md
echo "iOS app" > mobile/ios/README.md
echo "How fees are calculated" > docs/fees.md
echo "Fee Tracker" > README.md
git add .
git commit --quiet -m "Set up the Fee Tracker monorepo"
git ls-files
Output
README.md
backend/src/FeeCalculator.java
backend/src/HostelFees.java
docs/fees.md
mobile/android/README.md
mobile/ios/README.md
web/pages/fees.html

Example 1: Keeping Only Two Folders

Advitha works on the back end and its documentation, so she keeps only those:

Terminal
git sparse-checkout set backend docs
ls
git status
Output
README.md
backend
docs
On branch main
You are in a sparse checkout with 58% of tracked files present.
 
nothing to commit, working tree clean

Explanation: web/ and mobile/ vanished from the working folder, while README.md, a top-level file, stayed: in the default cone mode Git always keeps the files directly in the top folder. git status reports how much of the project is present, and that nothing was changed. The files weren't deleted from the repository: they are still in every commit, and git ls-files still lists them.

Terminal
git sparse-checkout list
git ls-files | wc -l | tr -d " "
Output
backend
docs
7
The working folder after a sparse checkout

Example 2: Working as Usual

Commits work exactly as before, and they keep the other folders untouched:

Terminal
echo "class TermFees {}" > backend/src/TermFees.java
git add backend
git commit --quiet --author="Advitha Anamaneni <advitha@example.com>" -m "Add term fees"
git show --stat --format="%s" HEAD
Output
Add term fees
 
 backend/src/TermFees.java | 1 +
 1 file changed, 1 insertion(+)

Explanation: the commit added one file and kept web/ and mobile/ exactly as they were. To other people, it is a normal commit. Branches can be switched as usual too; Git checks out only the selected folders.

Example 3: Adding a Folder and Turning It Off

Terminal
git sparse-checkout add web
ls
git sparse-checkout disable
ls
Output
README.md
backend
docs
web
README.md
backend
docs
mobile
web

Explanation: add put web/ back next to the existing selection, and disable restored every folder. set replaces the selection, while add extends it.

Example 4: A Sparse Clone

The biggest saving comes from combining sparse checkout with a partial clone (see "Shallow and Partial Clones"): Git then downloads only the file contents of the folders you choose. A bare copy stands in for the server:

Terminal
cd ..
git clone --quiet --bare fee-tracker fee-tracker.git
git -C fee-tracker.git config uploadpack.allowFilter true
git clone --quiet --filter=blob:none --sparse file://$PWD/fee-tracker.git srihitha
cd srihitha
ls
git sparse-checkout set mobile/android
ls mobile
git rev-list --all --objects --missing=print | grep -c "^?"
Output
README.md
android
6

Explanation: --sparse checked out only the top-level files, so the clone started with just README.md. Then set mobile/android brought in one folder, and Git fetched only its file. The other 6 files' contents were never downloaded (the ? lines), and the full history is still there.

Cone Mode and Patterns

Cone mode, the default since Git 2.37, selects whole folders. It is fast because Git can decide by folder name alone. The older non-cone mode takes patterns like .gitignore, such as *.java or !docs/. It is more flexible and much slower on big repositories, and Git's documentation no longer recommends it.

Cone mode (default)Non-cone mode
SelectsWhole folders, plus top-level filesAny pattern, like .gitignore
Speed in huge repositoriesFastSlow
Turned on bygit sparse-checkout set <folder>git sparse-checkout set --no-cone <pattern>
RecommendedYesOnly for unusual needs

Common Mistakes

Thinking the hidden files were deleted. They are only not checked out. git sparse-checkout disable brings them back, and they are still in every commit.

Editing a file outside the selection with another tool. Git normally doesn't look outside the selection. If you need a file, add its folder first.

Using set when you meant add. set replaces the whole selection.

Expecting sparse checkout alone to save download time. On its own it only saves disk space and speeds up local commands. Combine it with --filter=blob:none to download less.

Under the Hood

Every entry in the index (the staging area) has a skip-worktree bit. Sparse checkout sets it for every file outside the selection: Git then leaves those files out of the working folder, and commands like git status and git add don't look for them, while commits still contain them exactly as in the parent. The selection is stored in .git/info/sparse-checkout, and the settings core.sparseCheckout and core.sparseCheckoutCone turn the feature on. A sparse index (git sparse-checkout set --sparse-index) goes further: instead of one index entry per hidden file, it stores one entry per hidden folder, so the index itself stays small in huge repositories.

Interview Corner

What is sparse checkout?

A feature that checks out only selected folders into the working folder, while the repository keeps every file. It is used in large monorepos to make the working folder smaller and commands faster.

What is the difference between git sparse-checkout set and add?

set replaces the selection with the folders you name, and add adds folders to the existing selection.

What is cone mode?

The default mode of sparse checkout, which selects whole folders (plus top-level files) instead of arbitrary patterns. It is much faster than the older pattern mode.

How do you clone only one folder of a big repository?

git clone --filter=blob:none --sparse <url>, then git sparse-checkout set <folder>. Git downloads all commits but only the file contents of that folder.

Practice

Challenge: only the web pages. In Navika's fee-tracker folder, keep only web/pages, then list the top-level entries and print the sparse-checkout list.

What does Git print? Before you open the solution, decide whether README.md stays and what list prints.

Show solution
Terminal
cd ../fee-tracker
git sparse-checkout set web/pages
ls
git sparse-checkout list
Output
README.md
web
web/pages

README.md stays, because cone mode always keeps top-level files, and web is there with only pages inside. list prints the folder exactly as selected.

Checkpoint. In any project with several folders, select one folder, make and commit a change, then run git sparse-checkout disable and confirm with git status that the other folders came back unchanged.

Quick check: sparse checkout

4 questions
1What does git sparse-checkout set backend do?
2Which command brings back every file?
3Which combination downloads the least for one folder of a huge repository?
4Where does Git store the selection?

Key Takeaways

Quick reference

CommandIt is used to
git sparse-checkout set <folder>...Keep only these folders
git sparse-checkout add <folder>...Add folders
git sparse-checkout listShow the selection
git sparse-checkout disableBring back every file
git clone --filter=blob:none --sparse <url>Clone for sparse work

Was this tutorial helpful?

Lesson 85 of 106