How do I replace a git submodule with another repo?
If the location (URL) of the submodule has changed, then you can simply:
- Modify the
.gitmodules
file in the repo root to use the new URL. - Delete the submodule folder in the repo
rm -rf .git/modules/<submodule>
. - Delete the submodule folder in the working directory
rm -rf <submodule>
. - Run
git submodule sync
. - Run
git submodule update
.
More complete info can be found elsewhere:
- Changing remote repository for a git submodule
What fixed this for me was in the root of your git repo (not the submodule), run
rm -rf .git/modules/yourmodule
Then you should be able to add as normal.
First, delete the current submodule with the method already mentioned here, which I'm including for convenience:
- Delete the relevant section from the
.gitmodules
file - Delete the relevant section from
.git/config
- Run
git rm --cached path_to_submodule
(no trailing slash) - Commit and delete the now untracked submodule files
Now, add the new submodule with the --name
flag. This will give git an alternate name to reference in .git/config
for the submodule, to deconflict with the submodule that was there historically, which you still want to work in your prior history.
So type:
git submodule add --name UpdatedTestFramework [email protected]:userB/TestFramework.git
and you'll get the submodule loaded at the path you expect.
These commands will do the work on command prompt without altering any files on local repository.
git config --file=.gitmodules submodule.Submod.url https://github.com/username/ABC.git
git config --file=.gitmodules submodule.Submod.branch Dev
git submodule sync
git submodule update --init --recursive --remote