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)
Submodules are very static and only track specific commits. Submodules do not track git refs or branches and are not automatically updated when the host repository is updated. When adding a submodule to a repository a new . gitmodules file will be created.
Once you have set up the submodules you can update the repository with fetch/pull like you would normally do. To pull everything including the submodules, use the --recurse-submodules and the --remote parameter in the git pull command .
Cloning a Project with Submodules If you pass --recurse-submodules to the git clone command, it will automatically initialize and update each submodule in the repository, including nested submodules if any of the submodules in the repository have submodules themselves.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With