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 git@github.com:userA/TestFramework.git
  • I'd like it to now point to git@github.com: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.
    

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

  • Modify your .gitmodule file to use the new URL
  • Run git submodule sync
  • More complete info can be found elsewhere:

  • Changing remote repository for a git submodule
  • How to change a git submodule URL

  • 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.


    这些命令将在不改变本地存储库上的任何文件的情况下执行命令提示符下的工作。

    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
    
    链接地址: http://www.djcxy.com/p/2696.html

    上一篇: 简单的方法来拉最新的所有git submodules

    下一篇: 如何用另一个回购库替换一个git子模块?