Nick Sergeant Nick Sergeant - 4 months ago 21
Git Question

How do I clone a subdirectory only of a git repository?

I have my git repo which, at the root, has two sub-dirs

/finisht
/static


When this was in SVN,
/finisht
was checked out in one place, while
/static
was checked out elsewhere, like so:

svn co svn+ssh://admin@domain.com/home/admin/repos/finisht/static static


Is there anyway to do this with git?

Answer

This is called a sparse checkout, available since version 1.7.0.

"Sparse checkout" allows populating the working directory sparsely. It uses the skip-worktree bit (see git-update-index) to tell Git whether a file in the working directory is worth looking at.

While $GIT_DIR/info/sparse-checkout is usually used to specify what files are in, you can also specify what files are not in, using negate patterns. For example, to remove the file unwanted:

/*
!unwanted

See the linked answer and manual for details.