Git — Use Sparse Checkout — Pull Specific Folder from Repository

Atul Singh
3 min readJan 31, 2019

What if your git repository has lots of folders but you have to work on a specific file in a particular folder. This git feature is called Sparse checkout. Previous Versions of git doesn’t support this feature which forces you to download the whole repository. Sometime repository is too big to download and time-consuming process.

Current git versions support Sparse checkout which allows you to clone or fetch only a particular folder from a very big repository. Let’s see how we can achieve it.

Task — Need to sync a folder named ‘other’ from ‘DataGenX’ repository

Step #1: Initialize the Repository
Create a folder where you want to sync your git repo folder and Initialize git

Step #2: Add the Remote Repository
Add the remote Git repository with this local git repo as below -

Step #3: Create and Checkout a branch [Optional Step]
Creating of the branch is a totally optional step but it is advisable to create.

Step #4: Enable the Sparse Checkout Properties
Now, we have to enable the Sparse checkout properties and adding the folder name (in our case — ‘other’) in property file which we want to sync.

Step #5: Pull the Specific Folder
This is the last step where we pull the specific folder as below -

git pull <remote> <pull_branch_name> #not locally created

while running this command, need to give proper branch name from where you want to pull the data, In our case, it is master.

Step #6: List and work with synced directory

Commands as below —

mkdir <dir>; cd <dir>; 
git init
git remote add origin <GIT_URL>
git checkout -b '<branch_name>'
git config core.sparsecheckout true
echo <dir1>/ >> .git/info/sparse-checkout
git pull origin <pull_branch_name> # branch which you want to pull

--

--