'git submodule add' without cloning it
Asked Answered
B

1

11

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.

Bremsstrahlung answered 3/3, 2017 at 16:38 Comment(2)
Possible duplicate of Is there a way to `git submodule add` a repo *without* cloning it?Edge
In my experience git submodules are only a good solution in very few cases and I found them cumbersome to actually work with. There might be better tooling today though. Question is, are you looking at the wrong hammer? Consider explaining in more detail what your problem is, and your toolset, and see if others can find a better solution.Ine
B
2

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

Barometer answered 23/4, 2019 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.