Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'git submodule add' without cloning it

How could I add a local submodule to a projet without actually cloning all the files it contain? I want to avoid duplicating my project's files locally.

I know this kind of thing can be achieved because when you clone a project that includes submodules, these are not cloned by default. You have to do it manually:

git clone url-of-repo-containing-submodules.git
git submodule init sub-mod
git submodule update --remote

Let say, I have a git repo meta-project with an out-source repo lib-sobmodule.

I could hack it to avoid file duplication:

cd /path/to/metaproject
git submodule add ../path/to/lib-sobmodule
git commit -m "lib-sobmodule added..."
git push
cd .. && rm -rf meta-project
git clone url-of-meta-project.git

Tadam! Files of lib-sobmodule are not duplicated on my desktop. But its not a nice solution as it exports then imports everything to/from a git remote...

Is there an option or a method that could prevent to clone a local project without having to duplicate all the files it contains?

Disclamer: This is almost a clone question of Is there a way to git submodule add a repo without cloning it?. But as the answers are focused on the 30K git submodules requirement, they did not gave a satisfactory solution for the more common use case I describe here.

like image 775
jvtrudel Avatar asked Mar 03 '17 16:03

jvtrudel


People also ask

How do I add a submodule to a repository?

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 initialize a git submodule?

If you already cloned the project and forgot --recurse-submodules , you can combine the git submodule init and git submodule update steps by running git submodule update --init . To also initialize, fetch and checkout any nested submodules, you can use the foolproof git submodule update --init --recursive .

Why you should not use git submodules?

Git submodules may look powerful or cool upfront, but for all the reasons above it is a bad idea to share code using submodules, especially when the code changes frequently. It will be much worse when you have more and more developers working on the same repos.


1 Answers

Their is no clean solutions since it is intended purpose to clone the module inside the main project. It does not make sense to add submodule which does not have remote without getting the associated source code. Maybe a cat .git into metaproject/submoduledir would convince you about this mechanic.

However i understand your purpose and i found 2 ways to "solve" this issue :

  • Do NOT add local submodule. Instead create a remote for the module and add this remote as submodule in your main project. As long as you do not init and update your submodules, the code won't be duplicate localy.

  • The second way is a bit more dirty but can be usefull if you don't want to create a remote for your submodule. You just need to add the submodule as you are already doing and then run git deinit command on this submodule. It will just make Git shut up as long as you don't init the submodule again

like image 136
PopHip Avatar answered Sep 16 '22 13:09

PopHip