Checkout of Git subfolders

Working with Git (, you create the remote master repository and it usual has folders with different scopes.
Lets suppose your remote repository is contains a folder set like: /docs, /mainproject, /microservice1, /microservice2 and /microservice3. You'd like to checkout /microservice1 and /docs/microservice1_doc.

In a deployment with multiple machines, each microservice could run on one different machine and the clone of the whole repository is just a disk space consumption.
The /docs could contain video demo and a lot of images for all microservices while you need to pull the few MB of  /microservice2.
The “sparse checkout” ( available from Git 1.7.0 lets to pull only the selected subfolders.

Step 1
Create a local empty repository and connect it to remote master, fetching all objects but don't perform their checkout:
git init <local_repository>
cd <local_repository>
git remote add -f origin <url_remote_master_repository_no_include_target_subfolder>

According to this example, the last command will become: 
git remote add -f origin

Step 2
Set git for sparse checkout:
git config core.sparseCheckout true

Step 3
Set the subfolders to checkout, creating and filling the sparse-checkout file in .git/info/.

echo "my_folder_to_checkout" >> .git/info/sparse-checkout

In this example:
echo "microservice1/" >> .git/info/sparse-checkout
echo "docs/microservice1_doc/" >> .git/info/sparse-checkout

Or use your preferred editor to fill sparse-checkout.

Only the code for microservice1 and its documentation from docs/microservice1_doc will be downloaded

Step 4

Download the selected folders:
git pull origin master

Leave a Reply

© 2014-2015 Paraimpu Srl . Powered by Blogger.