MacBook Pro affichant un éditeur de code

Git sparse checkout: check out only one folder of a repository

Git sparse checkout lets you check out only part of a repository instead of its whole content. It is very handy when a repository is large and you only need one or two folders.

The modern method (git 2.25 and later)

Since git 2.25, a dedicated git sparse-checkout command makes this much simpler. The most efficient approach is to combine a partial clone with sparse mode.

git clone --filter=blob:none --sparse <url>
cd <repo>
git sparse-checkout set some/folder another/folder

The --filter=blob:none option avoids downloading the content of the files you do not need, and --sparse checks out only the root files to begin with. The git sparse-checkout set command then defines which folders to fetch.

On a repository that is already cloned, the steps are the same without the clone:

git sparse-checkout init --cone
git sparse-checkout set some/folder another/folder

Cone mode

The --cone option (enabled by default since git 2.27) speeds git up by restricting the selection to whole folders rather than arbitrary patterns. It is the recommended mode in the vast majority of cases. To see which folders are currently included:

git sparse-checkout list

And to go back to a full checkout:

git sparse-checkout disable

The old method (git before 2.25)

On an old version of git, you have to go through the manual configuration. You create an empty repository, add the remote and enable the option:

mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>
git config core.sparseCheckout true

You then list the folders you want in the .git/info/sparse-checkout file:

echo "some/folder/" >> .git/info/sparse-checkout
echo "another/sub/folder/" >> .git/info/sparse-checkout

Then update the working copy (replace main with master if your repository still uses that branch):

git pull origin main

Going further

The official git sparse-checkout documentation lists all the available options.

See also