How to clone only some directories from a git repository?

dobey's answer is no longer the case since git v1.7. You can now checkout certain folders from a repository. The full instructions are found here.

git init <repo>
cd <repo>
git remote add -f origin <url>

git config core.sparseCheckout true

echo "some/dir/" >> .git/info/sparse-checkout
echo "another/sub/tree" >> .git/info/sparse-checkout

This tells git which directories you want to checkout. Then you can pull just those directories

git pull origin master

First, do:

git clone --depth 1 [repo root] [name of destination directory]

Then:

cd [name of destination directory]

...And lastly:

git filter-branch --prune-empty --subdirectory-filter [path to sub-dir] HEAD

It's that easy. Git will rewrite the repo so that only the desired sub-dir is included. This works even if the sub-dir is several layers deep. Just name the destination directory the name of the sub-dir. Then in the "git filter-branch" command put the relative path to the sub-dir. Oh, the --depth 1 tells git to only download the top of the head (essentially removing the history).


You cannot. With git, you clone the entire repository, and the full history of the repository.

There are some workaround solutions to be able to get a single file out of a git archive, listed on a Stack Exchange answer for the same question, but you will still have to download the entire repository to get that single file or directory you want.

Tags:

Git