Checkout Specific Files From Git

Overview

From Git 1.7 it is possible to checkout specific files from a repository. The feature in Git is called sparse checkout.

How To

Create a repository:
git init myRepo

where myRepo is the directory to create the new repository.

Go into the repository and tell it will use the sparse checkout feature:
cd myRepo
git config core.sparseCheckout true

Create a file called sparse-checkout in .git/info/:
vi .git/info/sparse-checkout

Add all the files you want to checkout using repository root as the starting point. For example:
vagrant-digitalocean/Vagrantfile

Add the remote repository address. For example using one of my Github repositories:
git remote add -f origin git@github.com:dannytsang/example.git

Change git@github.com:dannytsang/example.git to your repository.

Set the branch:
git checkout master

The above should automatically pull the files down for only the specified files. Otherwise perform a git pull.

Summary

It’s not as easy to setup but once complete it makes it very easy to maintain.

Is it possible to do a sparse checkout without checking out the whole repository first?

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in SCM and tagged , , , . Bookmark the permalink.

Leave a Reply