How do I replace a git submodule with a different git repo?
Specifically, I have a submodule:
./ExternalFrameworks/TestFramework
First, delete the current submodule with the method already mentioned here, which I'm including for convenience:
.gitmodules
file.git/config
git rm --cached path_to_submodule
(no trailing slash)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 git@github.com:userB/TestFramework.git
and you'll get the submodule loaded at the path you expect.
The easiest way that I found is this:
git rm -rf [submodule_dir]
git submodule add --name new_[submodule_name] [new_submodule_url] [submodule_dir]
I didn't like the idea to modify my .gitmodules
manually. I also wrote a little blogpost about it.
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
If the location (URL) of the submodule has changed, then you can simply:
.gitmodule
file to use the new URLrm -rf .git/modules/<submodule>
rm -rf <submodule>
git submodule sync
git submodule update
More complete info can be found elsewhere:
If you want to change the remote URL only for this clone:
git config submodule."$submodule_name".url "$new_url"
This won't affect the .gitmodules
file in the parent project, so it won't be propagated to other developers.
This is described as "user specific record changes" here.
Do not run git submodule sync
as that will reset to the default URL again.
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.