Git is unavoidable for versioning changes in a project and improving the collaboration between many developers. It is also behind the rise of open source software, where people can build software with the source code accessible to everyone.
Clone a repository consists in making a copy of the remote repository to your local computer. However, sometimes we may want to clone only a specific folder in a repository.
The source code related to each article on this blog is stored in a single Git repository on GitHub.
A user reading a blog post and wanting to clone just the folder in the repository usually clones the whole repository and picks what they need. I do the same when consuming the blog posts of other authors.
Git sparse checkout
Starting the version 2.25.0, Git introduced a nice feature called
sparse-checkout combined with other commands, allows the cloning of a single folder in a Git repository.
Here is what the repository looks like:
Let's say we want to clone the folder
Make sure you have a version of Git higher than 2.25.0; here is the download link.
On your local computer, run the commands below:
git clone --no-checkout https://github.com/tericcabrel/blog-tutorials.git
git sparse-checkout init --cone
git sparse-checkout set deploy-node-app
git checkout @
git sparse-checkout init --cone initialize the folder with only the file at root directory (except the .git directory). For this repo, we only have one file: README.md.
git sparse-checkout set deploy-node-app pick the folder
deploy-node-app and add it in the repo. Now we the Readme file and the folder of the application we want to use.
Check the cloned repository to verify you only have the desired folder.
Check out the documentation to learn more about the sparse checkout.
If you are working with Monorepo, you can use sparse-checkout to speed up you CI/CD pipeline. Check out this great article on the GitHub blog.