Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a specific folder from a git repo as a git submodule?

I have a git repo and I want to add a submodule to it. Problem is, the submodule exists as a folder inside another repo. Can I add only that folder as a submodule?

like image 351
tuxcanfly Avatar asked Jul 01 '10 14:07

tuxcanfly


People also ask

How do I add a submodule to a specific path?

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 I clone a specific folder from a git repository?

In your test repository try creating an additional file at the top level. If you follow your instructions, then you'll also get a copy of that file as well as the directory you want. Remove the 'git sparse-checkout init --cone' but follow all your other instructions, and you'll just get the directory tree you want.


2 Answers

I ended up doing this:

  1. Create a submodules directory.
  2. Add the submodule in this directory.
  3. Create a symlink to the specific directory inside the submodule.

This way you have default Git submodule behaviour and in your project you only use a subset of the whole submodule.

like image 106
gitaarik Avatar answered Sep 28 '22 03:09

gitaarik


If you really need to include part of an other repository within the history of your own repo, then the subtree merge strategy is more adequate than submodules.

But in both case, the full repository is linked to your repo, not just one directory.
And partial cloning is not possible.

You could try and isolate that directory in its own repository, and then add it as a submodule, but that means its history will be totally seperated from the repo its was coming from originally.

like image 31
VonC Avatar answered Sep 28 '22 01:09

VonC