GIT sparse checkout

  • Published in soft
  • Date 2025-04-14
  • 2 min read

This git feature is nothing new, but I just discovered it and now don't know how I lived without it.

This feature allows you to save data and space by checking out only part of a large GIT repository you interested in exploring.

We'll use Gatsby for example. The repository contains a bunch of official plugins in ./packages. Let's say you're only interested in 'gatsby-plugin-image' and 'gatsby-plugin-sharp' because you're trying to make a gallery for your blog. Or I'm trying to make a gallery for my blog. I don't know how to write a blog, do I?

First, we could do:

git clone --no-checkout --depth=1 https://github.com/gatsbyjs/gatsby.git

We used --no-checkout to prevent automatic checkout after clone; and --depth=1 to create a 'shallow' clone without history.

Right now the cloned repository is empty:

cd gatsby
ls -a
.  ..  .git

Now we can set up sparse-checkout with the directories we're interested in:

git sparse-checkout set packages/gatsby-plugin-image packages/gatsby-plugin-sharp
git checkout
ls packages
gatsby-plugin-image  gatsby-plugin-sharp

Additional paths (or patterns) can be added with sparse-checkout add:

git sparse-checkout add packages/gatsby-transformer-sharp
ls packages
gatsby-plugin-image  gatsby-plugin-sharp  gatsby-transformer-sharp

Another use case, for example, could be checking out only the backend from repository containing both the backend and frontend.

WIP

©2025 barelyan.art