Git sparse checkout

How to clone only specific files from git repository

tldr

git clone --filter=blob:none --no-checkout --depth 1 <repository-url>
cd <repo-name>
git sparse-checkout init --no-cone
git sparse-checkout set '/*' '!/src/assets/'
git checkout

explanation

Sometimes git repositories are large with gigabytes of files we don’t want to download. Especially when all we want to do is make a small patch, or copy only specific folder and nothing else. Git sparse checkout with partial clone is a solution that allows us to clone only specific files from a repository.

First we perform shallow clone without checkout or downloading any files

git clone --filter=blob:none --no-checkout --depth 1 <repository-url>
  • --filter=blob:none - do not download any files
  • --no-checkout - do not checkout
  • --depth 1 - (optional) clone only the latest commit, helps with big history

Then we go into cloned repository

cd <repo-name>

Now we need to initialize sparse checkout with --no-cone - old-style pattern matching that allows negative filters

git sparse-checkout init --no-cone

Now we set patterns for sparse checkout. /* means include everything, !/src/assets/ means exclude /src/assets directory.

git sparse-checkout set '/*' '!/src/assets/'

Finally we checkout files, this will start download

git checkout

notes

  • Check more in the docs: https://git-scm.com/docs/git-sparse-checkout
  • You might also want to check default --cone - it’s a high performace option for cases where running your filter patterns over millions of files would be too slow. But it’s way less flexible - no negative filters and it downloads all files in root folder right away and all files in directories you add.