DEV Community

Christophe Colombier
Christophe Colombier

Posted on

git sparse-checkout

Yesterday, I discovered git sparse-checkout feature with @sanixdarker post

His article is good, and helped me to discover the feature.

Here I would like to log for archive what I found about git sparse-checkout.

What is git sparse checkout

Here is what git documentation tells about it.

Reduce your working tree to a subset of tracked files

So if you have this kind of repository

README.md
CHANGELOG.md
src/
assests/
tests/
web/
tools
Enter fullscreen mode Exit fullscreen mode

And you want to fetch only src and tests folders, use this.

src/
tests/
Enter fullscreen mode Exit fullscreen mode

Because other folders are irrelevant to your needs.

why would you need this ?

When running CI, you don't need assets (images, css ...) it could speed up the git clone on a huge repository

BUT you can add commit and work as normal. You simply didn't fetch the information of the other folders.

Interesting reads

First, I've read this very interesting article

Faster git clones with sparse checkouts | Aymeric Beaumet

We at REKKI are working on a monorepo that contains all the backend Go code for most of our services and jobs. As time goes by and the size of this repository grows, the time it takes for an initial…

favicon aymericbeaumet.com

EDIT: you can also find an article on dev to from the author himself @aymericbeaumet

and of course the documentation https://www.git-scm.com/docs/git-sparse-checkout

How to use it ?

Start with a git clone minimal fetch

git clone --filter=blob:none --no-checkout <repository>
Enter fullscreen mode Exit fullscreen mode

It's possible to configure a repository with nothing in it, yet (the yet is important)

  • --filter=blob:none: instructs not to fetch any blob object
  • --no-checkout: instructs not to automatically checkout HEAD

so here you can git checkout your branch, and it will bring nothing different from if you didn't use --no-checkout

but you can use git sparse-checkout instead.

Sparse checkout

So remember, we only want src and tests folders.

So do this:

git sparse-checkout set src tests
Enter fullscreen mode Exit fullscreen mode

Tips you can use --stdin

you can also use --stdin

echo -e "src\nassets" | git sparse-checkout set --stdin
Enter fullscreen mode Exit fullscreen mode

Note: Here is used echo as an example, using --stdin means you are scripting your usage.


Note: You can also add as you go or need

git sparse-checkout set src
# ...
# and later, even days later
git sparse-checkout add tests
# and here it would fetch the missing files
Enter fullscreen mode Exit fullscreen mode

Note: You can also use --stdin with git sparse-checkout add also

Tips: use git sparse-checkout list to know what you are tracking

You can use git sparse-checkout list at any time to see what is configured.

git sparse-checkout list
Enter fullscreen mode Exit fullscreen mode
src
tests
Enter fullscreen mode Exit fullscreen mode

How does git follow this internally

you can find the configured things in .git/info/sparse-checkout file

  /*
  !/*/
  /src/
  /tests/
Enter fullscreen mode Exit fullscreen mode

Note: there are other files involved in sparse checkout, you can read the documentation.

Warning: I would recommend to use git sparse-checkout command than playing with those files directly.


So what, then ?

well you can simply fetch and checkout your branch, only the requested folders or files will be present.

README.md
CHANGELOG.md
src/
tests/
whatever/
foobar/
Enter fullscreen mode Exit fullscreen mode
git sparse-checkout set src tests
Enter fullscreen mode Exit fullscreen mode

then if you check out the main branch, you will get this.

git checkout main
Enter fullscreen mode Exit fullscreen mode
README.md
CHANGELOG.md
src/
tests/
Enter fullscreen mode Exit fullscreen mode

Note: by default, all the files (files only, not folders) at the root of your repository will always be checkout. You can see the reason of this on the documentation.

if you don't want this, you will have to use a legacy feature via --no-cone parameter, when setting up your sparse checkout

git sparse-checkout set --no-cone src tests
Enter fullscreen mode Exit fullscreen mode

then if you check out, you will get this.

src/
tests/
Enter fullscreen mode Exit fullscreen mode

I hope you will find this useful.

Oldest comments (4)

Collapse
 
sanixdarker profile image
darker

Great article mate !!!

Collapse
 
ccoveille profile image
Christophe Colombier

Thanks, I just rewrote it a bit, I posted it a bit fast :P

Collapse
 
ccoveille profile image
Christophe Colombier • Edited

When you discover that the author of the article you are quoting is not only on dev.to : @aymericbeaumet

But he also wrote, an article on dev.to about it:

Thank you guy, I loved your article, I will follow you for now

Collapse
 
aymericbeaumet profile image
Aymeric Beaumet

Glad you liked it! Great article