akadir's blog

git sparse checkout

April 20, 2020

Today I have learned that there is an option to fetch only specific folders or files from remote git repository. Yes, you are right, it seems like the opposite of .gitignore.

So, how do we use this?

Let’s say we will fetch remote repository that structured as shown in below:

.
├── folder-we-want
│   ├── file-1.jar
│   └── file-2.jar
├── folder-we-do-not-want
│   ├── other-file.python
│   └── another-file.cpp
└── README.md

But we only want to fetch folder-we-want’s content and README.md file. To do that, first create a directory to fetch this imaginary repository and run git init and add our remote url.

mkdir imaginary-repo
cd imaginary-repo
git init
git remote add –f <imaginary-repo-url>

After that we need to enable sparse checkout using this command:

git config core.sparsecheckout true

Now we can add folder path we do not want in our local repository into .git/info/sparse-checkout starting with exclamation mark. Our updated.git/info/sparse-checkout file content should be as below:

!folder-we-do-not-want

Note: in git-sparse-checkout documentation it is stated that, by default, sparse-checkout file uses the same syntax as .gitignore files. While this file is usually used to specify what files are included, what files are not included can also be specified by using negative patterns.

After all these modifications, when we fetch the repository with git pull <remote> <branch> command our local directory(imaginary-repo)‘s structure should be as below:

├── folder-we-want
│   ├── file-1.jar
│   └── file-2.jar
└── README.md

Thanks to sparse checkout, we do not have the folder named folder-we-do-not-want.

We also can use sparse-checkout in existing local repositories as below. The only difference, after enabling sparse checkout and updating the .git/info/sparse-checkoutfile, we should run one extra command:

git config core.sparsecheckout true
echo folder-we-do-not-want >> .git/info/sparse-checkout
echo another/unnecessary/folder >> .git/info/sparse-checkout
git read-tree -mu HEAD

You can read detailed documentation from here: git-sparse-checkout and more detailed and experimental blog post from here: Bring your monorepo down to size with sparse-checkout


akadir

Personal blog by akadir.