How do I replace a git submodule with another repo?

GitGit Submodules

Git Problem Overview


How do I replace a git submodule with a different git repo?

Specifically, I have a submodule:

  • located at ./ExternalFrameworks/TestFramework that points to a git repo [email protected]:userA/TestFramework.git
  • I'd like it to now point to [email protected]:userB/TestFramework.git.

The problem is that when I delete the submodule with the method described here, then re-add it using the command

git submodule add git@github.com:userB/TestFramework.git

I get this error:

A git directory for 'ExternalFrameworks/TestFramework' is found locally with remote(s):
  origin	git@github.com:userA/TestFramework.git
If you want to reuse this local git directory instead of cloning again from
  git@github.com:userB/TestFramework.git
use the '--force' option. If the local git directory is not the correct repo
or you are unsure what this means choose another name with the '--name' option.

Git Solutions


Solution 1 - Git

If the location (URL) of the submodule has changed, then you can simply:

  1. Modify your .gitmodule file to use the new URL
  2. Delete the submodule folder in the repo rm -rf .git/modules/<submodule>
  3. Delete the submodule folder in the working directory rm -rf <submodule>
  4. Run git submodule sync
  5. Run git submodule update

More complete info can be found elsewhere:

Solution 2 - Git

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 git@github.com:userB/TestFramework.git

and you'll get the submodule loaded at the path you expect.

Solution 3 - Git

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

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjoseph.hainlineView Question on Stackoverflow
Solution 1 - GitTim HeniganView Answer on Stackoverflow
Solution 2 - Gitjoseph.hainlineView Answer on Stackoverflow
Solution 3 - GitPavan Sokke NagarajView Answer on Stackoverflow
Solution 4 - GithobberwickeyView Answer on Stackoverflow
Solution 5 - GitDerZyklopView Answer on Stackoverflow
Solution 6 - GitjoeytwiddleView Answer on Stackoverflow