git submodule init does absolutely nothing
Asked Answered
A

4

40

I have a strange problem with "git submodule init"

When I added the submodules using "git submodule add url location" it cloned the repository just fine and everything was ok.

When I pushed all my changes back to the parent repository, added the .gitmodules files, etc and cloned the repository back, I tried to initialise all the submodules using "git submodule init"

And nothing happens :( Literally nothing, no output, no extra files, it does not even attempt to do anything actually.

So I am wondering, what did I do wrong?

.gitmodules:

bash$ cat .gitmodules
[submodule "projects/subprojectA"]
    path = projects/subprojectA
    url = ssh://[email protected]/test/projectA.git

[submodule "projects/subprojectB"]
    path = projects/subprojectB
    url = ssh://[email protected]/test/projectB.git
Assassin answered 29/9, 2016 at 8:51 Comment(0)
A
51

ok, I have figured out what I did wrong.

When I added the git submodules, I did a git status and it told me three things had changed

.gitmodules
projects/subprojectA
projects/subprojectB

when I was pushing all my changes to the repository, I didnt want to commit the submodules, cause I thought it would add all the files I just cloned, so I just did a git add .gitmodules and committed and pushed that.

But this is wrong, you need to do a git commit and commit everything it tells you, then when you do this, git registers those paths and when you clone, it will work.

but if you do not commit those folders, it wont register them and wont clone them when you clone the parent repository.

so that was my mistake, I misunderstood that adding those directories would add all the submodules code to the parent repository, I tried to sidestep that and it stopped working.

so just add your submodules and commit the results, it will all work out just fine :D

Thanks for Protectators help, regardless!

Assassin answered 29/9, 2016 at 9:46 Comment(3)
Thanks. I am using Submodules in GitLab and their documentation doesnt mention this at allPsycho
Thanks! Does anyone know why git subodule does it this way? Between .gitmodules, .git/modules and needing to commit them like this, the module registration seems very redundant.Anarchy
So if this happens the file is missing which points to the commit id of the submodule. The easiest way to fix is to re-add the submodule.Curriculum
M
20

Following worked for me in case of the googletest submodule:

git submodule add --force https://github.com/google/googletest.git googletest

So, you need to manually add submodule (note the --force flag).

Marchesa answered 28/7, 2019 at 9:41 Comment(1)
also, you need to remove the dest folder. At least that works for me.Legere
A
7

Use the --recursive option when cloning. This option initializes all listed submodules :

git clone --recursive

In another case, if you want to initialize submodules of an git you just cloned, you can use

git submodule update --init --recursive
Aspirate answered 29/9, 2016 at 8:53 Comment(3)
git submodule update --init --recursive does nothing here, no cloning, nothing, I am going to try git clone --recursive nowAssassin
git clone --recursive does not clone the submodules either, I am using git version 2.10.0Assassin
yup, the one from my comment, is the one I have when I clone the repository, thats why I am a bit puzzled....Assassin
A
0

I faced a similar problem, the issue was that VSCode did not show the submodule directory as an untracked file for some reason after I added the submodule. Therefore it wasn't committed into the repo. But git status showed that it's untracked. So you have to add and commit it like the accepted answer says.

Git will warn you with yellow text, that directly adding a repo like this might not be what you want. But the .gitmodules file is already up there, so if you commit and push this, new clones of the remote repo with submodules will work as intended.

If you pull the changes into an existing clone you will get an error that the submodule is not accessible, so you need to run git submodule init and git submodule update to get the module cloned and checked out in the desired state.

Ailurophobe answered 8/6 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.