Nested git repositories with github using submodule (in three minutes)

created: Nov. 9, 2009, 7:49 p.m.
tags: detached head git github git-submodule nested repositories pinax

You have a project that is a git repository. You want to pull some things into this project. Things that are also git repositories. You want these submodules to maintain their independent version control. You want to be able to update the whole project and all of the submodules at will.

Check out the docs on submodule.

And the section in the user's maual.

Here's the short version:

git submodule add git://github.com/skyl/django-olwidget.git apps/olwidget
git submodule add git://github.com/skyl/django-world.git apps/world
git submodule add git://github.com/skyl/django-geoms.git apps/geoms

This associated the urls of the submodule repositories with the relative paths where these submodules exist in the greater project. I had already checked them out there. So git told me, for example:

Adding existing repo at 'apps/world' to the index

So, from the submodule repo, can we run the usual commands and interact with the submodule remote? No:

git pull
git add .
git commit
git push

OOPS! I added the public repo instead of my privileged url (using github):

Use git@github.com:skyl/django-world.git

Not to worry, I can edit the .gitmodules file that was created to use my urls. And also manually, the .git/config files in the repositories if need be. For good measure, I ran:

git submodule sync

when I was happy with the urls. Perhaps this would have been a shortcut to editing manually?

I try the old add, commit, and push again after making some test changes to one of the submodules. But after, commit, I notice:

[detached HEAD d7c6bf9] detached

Then, when I try to push:

Everything up-to-date

There was nothing to push because I am not actually on a branch! If you don't want to create a new branch from the submodule you can run:

git checkout master

Then add, commit and push and you can update the submodule's remote repository. If you want to update the parent repo to fetch this new commit, check git diff for the parent. You should see something like:

-Subproject commit 16c9475d41e6aeb7c77bdb0f39251ad227a64638
+Subproject commit 5c2c88ba2248201ae8299494da43d02488c9ddae

You can just add, commit and push as normal.

comments powered by Disqus