Search code examples
gitsubdirectorysvn-externalsgit-subtree

Git: clone/subtree/filter only sub-directory of 3rd party lib


svn:externals "http://svn.3rdapp.com/project/subdir/subdir/ lib/mydir/subdir"

SVN allows developers to include sub-directories of 3rd party libraries into sub-directories of their own repo.

I'd like to include only a sub-directory of the 3rd party lib into my project, likely as a subtree which can be edited and new upstream changes merged with my edits.

Specifically https://github.com/openid/php-openid/tree/master/Auth included into Vendor/Auth without any of the files or history from the other directories.


Solution

  • You can use the sparseCheckout feature of Git which was introduced in version 1.7.0. It allows you to include specific sub directories of a repository in your project. Try this following steps:

    1. git clone https://github.com/openid/php-openid.git Vendor/Auth
    2. cd Vendor/Auth
    3. git config core.sparsecheckout true
    4. echo Auth/ > .git/info/sparse-checkout
    5. git read-tree -m -u HEAD

    In step #4 above, Auth subdirectory (which you want to use) is added to the .git/info/sparse-checkout file.

    If later your decide to change which subdirectory you would like to use then just edit the sparse-checkout file and redo step #5.