如何用另一个repo替换git子模块?

问题描述 投票:85回答:6

如何用不同的git repo替换git子模块?

具体来说,我有一个子模块:

问题是当我用here描述的方法删除子模块时,然后使用命令重新添加它

git submodule add [email protected]:userB/TestFramework.git

我收到此错误:

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.
git git-submodules
6个回答
109
投票

如果子模块的位置(URL)已更改,那么您可以简单地:

  1. 修改您的.gitmodule文件以使用新的URL
  2. 删除repo rm -rf .git/modules/<submodule>中的子模块文件夹
  3. 删除工作目录rm -rf <submodule>中的子模块文件夹
  4. 运行git submodule sync
  5. 运行git submodule update

更完整的信息可以在其他地方找到:


32
投票

首先,使用已经提到的here方法删除当前子模块,为方便起见,我将其包括在内:

  • .gitmodules文件中删除相关部分
  • .git/config删除相关部分
  • 运行git rm --cached path_to_submodule(没有尾随斜杠)
  • 提交并删除现在未跟踪的子模块文件

现在,添加带有--name标志的新子模块。这将为git提供一个替代名称,以便在.git/config中为子模块引用,以消除历史上存在的子模块,您仍然希望在之前的历史记录中工作。

所以输入:

git submodule add --name UpdatedTestFramework [email protected]:userB/TestFramework.git

并且你将在你期望的路径上加载子模块。


6
投票

这些命令将在命令提示符下完成工作,而不会更改本地存储库上的任何文件。

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

5
投票

为我修复此问题的原因是你的git repo(不是子模块)的根目录,运行

rm -rf .git/modules/yourmodule

然后你应该能够正常添加。


2
投票

我发现最简单的方法是:

git rm -rf [submodule_dir]
git submodule add --name new_[submodule_name] [new_submodule_url] [submodule_dir]

我不喜欢手动修改我的.gitmodules的想法。我还写了关于它的a little blogpost


1
投票

如果要仅为此克隆更改远程URL:

git config submodule."$submodule_name".url "$new_url"

这不会影响父项目中的.gitmodules文件,因此不会传播给其他开发人员。

这被描述为“用户特定记录更改”here

不要运行git submodule sync,因为它会再次重置为默认URL。

© www.soinside.com 2019 - 2024. All rights reserved.