GIT - Just checkout parts of a git repository

If you want to include a submodule fully into your sourcetree (because you are forced to as in Unity3d) you can have some kind of workaround using a sparse-checkout (EDIT: with drawback...see bottom)

Just do it like this:

  • create git-project (if not done yet):
git init
mkdir Project/Assets/3rd
cd Project/Assets/3rd
# I take Zenject dependecy-injection framework as sample
git submodule add https://github.com/modesttree/Zenject.git 
# create the spare-checkout file (something like a .gitignore for checking out with white-/blacklist capabilities 
cd .git/modules/Project/Assets/3rd

In this folder create the file: sparse-checkout with following content:

/UnityProject/Assets/Plugins/Zenject/Source

Prefixed with /: included

Prefixed with !: excluded

Here I included only the Source-Folder. The directory structure stays as it is, but checkout out only the wanted files.

Last thing to do tell git to use sparse-checkout(from within the submodule):

git config core.sparseCheckout true

and of course checkout (in the submodule-folder)

git checkout

And as a disclaimer: I'm just started using it, not sure everything turns out as I wanted ;)

EDIT: Actually you cannot commit this state in any way, so you will have to repeat this everytime you clone your repository or write some helper script to do this for you. Too bad...still looking for the best way. Still this looks still good enough