Use git submodule absorbgitdirs
This is what the docs state this command does:
If a git directory of a submodule is inside the submodule,
move the git directory of the submodule into its superprojects
$GIT_DIR/modulespath and then connect the git directory and
its working directory by setting thecore.worktreeand adding
a .git file pointing to the git directory embedded in the
superprojects git directory.
So instead of starting all over as suggested in the previous answers by @DomQ and myself, one can just add run the following:
- Without removing from the index the submodule, Add the submodule’s url to
.gitmodulesand to.git/configwith
git submodule add <url> <path> - (Optional but recommended step) Move the submodule’s
$GIT_DIRdirectory (.gitin regular repositories) to.git/modules/<path>with:
git submodule absorbgitdirs <path>
Original answer – pre v2.12.0
git submodule absorbgitdirs was introduced only in v2.12.0-rc0 (see commit).
The Solution is quite simple. It was extracted from here.
git rm submodule-dir
This will delete all the files that git was tracking after insubmodule-dirrm -rf submoduledir
This will delete all the other files that might have been left insubmodule-dirbecause git ignored them.- Now, we have to commit in order to remove the files from the index:
git commit
After the commit, we cleaned the files that git followed and didn’t followed insubmodul-dir.
Now it’s time to do: git submodule add <remote-path-to-submodule>
This will re-add the submodule but as a true submodule.- At this point it might be a good idea to check
.gitmodulesand see if the submodules have been added successfully. In my case I already had an.gitmodulesfile so I had to modify it.