How to change a git submodule to point to a subfolder?

ripper234 picture ripper234 · Mar 14, 2011 · Viewed 57.4k times · Source

Skimming through the SubModule tutorial, I created a submodule out of the boto project. Then, I discovered that I actually need only a subset of this project - specifically, the boto folder.

I would like to change my submodule to point to this folder. When I look in .gitmodules, I see

[submodule "backup/src/boto"]
    path = backup/src/boto
    url = https://github.com/boto/boto.git

What URL should I use instead of https://github.com/boto/boto.git? After I change the URL, should I delete the boto folder locally and re-pull?

Answer

Mark Longair picture Mark Longair · Mar 14, 2011

I'm afraid the URL for submodules always just points to the repository - you can't specify that you only want a subfolder of a repository, in the same way that git doesn't support "narrow clones" in general.

If you can't live with having the whole repository as a submodule, you could always create a new repository that's cloned from boto and then set up a cron job to:

  1. git fetch that repository into a directory
  2. Use git filter-branch to update a branch where the subdirectory is at the top level.
  3. Add that branch of the repository as the submodule. However, that's all a bit fiddly and my preference would just be to live with having the whole repository as a submodule.