Cloning only a subdirectory with git

user1526912 picture user1526912 · Aug 6, 2012 · Viewed 78.3k times · Source

I have a git repository that includes subdirectories. Example dirA/dirB. Is there any way to do a git clone on a Unix server to pull only files from a subdirectory (dirB)?

Is there some other git command other than clone that will do this?

How do I git a specific file?

Answer

unutbu picture unutbu · Aug 6, 2012

Suppose your project is in a dir called project, and you want only those commits which touch project/dirB.

Then:

git clone project/ subproject/
cd subproject
git filter-branch --prune-empty --subdirectory-filter dirB HEAD 

subproject will now contain the git history which touches dirB.