How do I replace a git submodule with another repo?

joseph.hainline picture joseph.hainline · Jan 18, 2013 · Viewed 43.1k times · Source

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 [email protected]:userB/TestFramework.git

I get this error:

A git directory for 'ExternalFrameworks/TestFramework' is found locally with remote(s):
  origin    [email protected]:userA/TestFramework.git
If you want to reuse this local git directory instead of cloning again from
  [email protected]: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.

Answer

Tim Henigan picture Tim Henigan · Jan 18, 2013

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: