Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I replace a git submodule with another repo?

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.
like image 569
joseph.hainline Avatar asked Jan 18 '13 17:01

joseph.hainline


People also ask

How do I move a submodule to a different directory?

Git Submodules Moving a submodule If needed, create the parent directory of the new location of the submodule ( mkdir -p new/path/to ). Move all content from the old to the new directory ( mv -vi old/path/to/module new/path/to/submodule ). Make sure Git tracks this directory ( git add new/path/to ).


3 Answers

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

  1. Modify the .gitmodules file in the repo root 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:

  • Changing remote repository for a git submodule
like image 88
Tim Henigan Avatar answered Oct 09 '22 20:10

Tim Henigan


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.

like image 37
joseph.hainline Avatar answered Oct 09 '22 20:10

joseph.hainline


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
like image 10
Pavan Sokke Nagaraj Avatar answered Oct 09 '22 20:10

Pavan Sokke Nagaraj