Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git Submodule to a subfolder

In svn you can link a repository to any folder in another svn repository. I'm wondering if there is a similar feature for git? Basically I want a git submodule inside my repository, but I want the submodule to be a pointer to a subfolder of another git repository, not the whole repository. Is this possible?

like image 250
Cory Avatar asked Jul 13 '09 18:07

Cory


People also ask

How do I add a submodule to a folder?

In order to add a Git submodule, use the “git submodule add” command and specify the URL of the Git remote repository to be included as a submodule. When adding a Git submodule, your submodule will be staged. As a consequence, you will need to commit your submodule by using the “git commit” command.

How do submodules work in git?

Use the git submodule update command to set the submodules to the commit specified by the main repository. This means that if you pull in new changes into the submodules, you need to create a new commit in your main repository in order to track the updates of the nested submodules.

How do I add a submodule to bitbucket?

You simply need to be in your root folder and then add the submodule folder. This option is only valid for the update command. Instead of using the superproject's recorded SHA-1 to update the submodule, use the status of the submodule's remote-tracking branch. This is equivalent to running git pull in each submodule.


3 Answers

Git does not support partial checkouts, so the submodule must point to a complete repository.

This thread on the Git mail list provides some background information.

This article from Panther Software offers some insight from someone else trying to accomplish a similar goal (i.e. replicate svn:externals using Git).

If both projects are under your control, then I suggest you isolate the "subfolder" that you interested in to its own standalone repo. Then both projects can create submodules that link to it.

like image 88
Tim Henigan Avatar answered Sep 27 '22 20:09

Tim Henigan


I'm running into the same issue. It doesn't look solvable from a git level, at least not in a way that lets you easily pull or push to the parent repo.

However, you can work around this limitation by using a simple symlink:

  1. Set up your submodule in a separate directory.
    • git submodule add http://example.com/repo.git ./submodules/repo
  2. Create a symlink to the subfolder in the place you want:
    • ln -s ./submodules/repo/subdirectory ./wherever/symlinked_directory

References:

  • http://git-scm.com/book/en/Git-Tools-Submodules
  • http://www.cyberciti.biz/faq/unix-creating-symbolic-link-ln-command/
like image 29
Anthony Michael Cook Avatar answered Sep 27 '22 20:09

Anthony Michael Cook


Usually when you're trying to extract a subfolder of some other project, that subfolder should be a separate project in the first place, and both parent projects should be referring to it.

You can extract such a subproject's history using a tool like git subtree. Then you can link the subtree back into your project using either git submodule or git subtree, whichever you prefer.

like image 44
apenwarr Avatar answered Sep 27 '22 19:09

apenwarr