A Git submodule always occupies a folder (directory) because it's actually a second, additional Git repository and as such it needs its own hidden .git
. (This was once an additional .git
folder; now it's a .git
file but everything else is still arranged as if it were a hidden .git
folder within the submodule folder.)
In command-line Git, then, you'd normally do this (I'll assume here that you're using GitHub and prefer to have GitHub make the first commit within each repository, then clone them, rather than the more complicated "make the repository locally, make a first commit, make an empty GitHub repository making sure not to make a first commit there, do some other more-complicated stuff, etc." sequence):
- Make the new repository on GitHub for the project itself.
- Make the new repository on GitHub for the submodule.
git clone ssh://git@github.com/me/proj.git
to make a clone named proj
for the project. (This assumes you're using ssh; adjust the URL for https if you are using HTTPS.)
cd proj
to enter the cloned project.
git submodule add ssh://git@github.com/me/lib.git lib
to tell Git to make a clone named lib
from ssh://git@github.com/me/lib.git
whenever it goes to get the submodule. Your own Git will now make that clone as well, and you now have everything set up.
Note that git submodule add
won't bother making a clone if the clone already exists. In that case you probably want to run git submodule absorbgitdirs
. See How to make an existing directory within a git repository a git submodule as well.