Adding git submodules automatically (.gitmodules)
Asked Answered
G

3

19

I have a repo with several submodules. I want to add some others, and the fastest way for me is to use the .gitmodules (which in my opinion should clearly allow any kind of submodule management).

However, when editing this file and adding submodules, after a git submodule init nothing is added (except the submodules that were already present before the modification).

Is there any solution to add a submodule without going through git submodule add (ie, just by editing the .gitmodules file and then git submodule update --init) ?

That is, the following workflow should automatically add the submodule "foo/bar":

Add the following to .gitmodules:
    [submodule "foo/bar"]
        path = foo/bar
        url = https://example.com/foo.git

Run the following command after saving:
    git submodule init
    git submodule update

Expected result:
    submodule 'foo/bar' automatically gets added
    it is also updated (the update command)
Greaser answered 16/7, 2014 at 10:5 Comment(5)
When you write git submodule add its simply adding the new entry to the .submodules files, so what you described is correct and it should work for you. this is why after the add you have to run init & updateMusetta
Not using git submodule add results in the module not being added for me (sadly)... When you think about it, git is able to track thousands of modifications in a lot of files, but totally lost when it comes to 3 added lines in .gitmodules...Greaser
I have updated my answer according to your update, you cannot do it manually unless you know how to create the module directory under .git folder.Musetta
execute this command and view the results GIT_TRACE=2 git submodule add [email protected]....Musetta
Quite crazy this is not possible in a text-oriented tool like this ^^Osmious
G
5

When you add a Git submodule, Git creates .gitmodules file and for a submodule named git-submodule will add something like this:

[submodule "git-submodule"]
    path = git-submodule
    url = https://github.com/TomasHubelbauer/git-submodule

The same is added to .git/config after the existing content in that file.

A folder for the submodule named after the submodule is created in .git/modules. This folder is nearly identical to the the .git directory of the actual submodule repository, but it doesn't contain the actual objects (instead the submodule data is checked out to its directory and its metadata are here).

This means that theoretically, you might be able to add a submodule by hand without using git submodule add, but you would have to recreate all these config files. But one can still imagine cloning the submodule repository to a separate directory and copying its .git over to this one. That might work.

However, adding a submodule also changes the index, .git/index, so you would have to manually update this hash as well, and at this point, you're reimplementing Git, but manually.

As a result, I don't believe it is anywhere near practical to add a Git submodule by hand.

Georgeta answered 10/12, 2019 at 19:5 Comment(2)
Thanks for your answer. The question was not really about how to add a submodule by hand though, it was about how to make the .gitmodule file the master of the submodule configuration, that is you edit it and type git submodule update to adjust the repo. Apparently, this is not possible :(Greaser
Ah, I see, I misunderstood that. I also wish that was the case!Kimmy
M
2

Yep, as you described once you add submodule its being added to your .gitsubmodule file.

But unless you know exactly what you do its much better to use the CLI command since there might be something that you not familiar with like:

Once you done editing your submodule file you will need to run:

git submodule init
git submodule update

Adding it manually will not work.

Run the add submodule and watch the .git folder changes. You will see a new folder named module with your submodule name.

This is why you should not do it manually.

Musetta answered 16/7, 2014 at 10:16 Comment(1)
Well, my question is not adding by using the command line. What I would like is git submodule init to automatically add non-staged modules that are present in gitmodules. Ie, that the gitmodules files dictates how git manages submodules (and not the inverse). Is it possible ?Greaser
F
1

Since it's established that Git manages .gitmodules but does not read new entries from it, here is a oneliner in case it can be helpful to anyone else facing this problem.

It goes through the .gitmodules file looking for lines starting with [whitespace]url = and executes git submodule add followed by the URL that follows. It assumes that you want to create submodules with the same names as the corresponding repositories.

cat .gitmodules | sed -n 's/^\s*url = \(\S*\) */git submodule add \1/p' | bash

Explanation:

cat .gitmodules          # output file content
sed -n                   # output only the relevant lines
's/regEx/replacement/p'  # print the 'replacement' of the 'regEx'
^\s*url =                # look for lines starting with [whitespace]url = 
                         # but not, e.g., with #[whitespace]
\(\S*\) *                # capture all characters, ignore trailing [space] 
git submodule add \1     # append the captured URL to the git command
bash                     # execute the command

Omit the | bash part to only print the commands without executing them. Modify the git command to your liking. After successful execution, you can do git submodule update --remote to checkout the branches/revisions that may be specified in .gitmodules.

Fussbudget answered 8/9, 2023 at 8:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.