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

时间:2021-03-16 07:33:55

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

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

Specifically, I have a submodule:

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

  • located at ./ExternalFrameworks/TestFramework that points to a git repo git@github.com:userA/TestFramework.git
  • 位于./ExternalFrameworks/TestFramework中,该框架指向git repo git@github.com:userA/TestFramework.git
  • I'd like it to now point to git@github.com:userB/TestFramework.git.
  • 现在我希望它指向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.

6 个解决方案

#1


105  

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

如果子模块的位置(URL)发生变化,则可以简单地:

  1. Modify your .gitmodule file to use the new URL
  2. 修改.gitmodule文件以使用新的URL
  3. Run git submodule sync
  4. git子模块同步运行

More complete info can be found elsewhere:

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

#2


32  

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
  • 从.gitmodules文件中删除相关部分
  • Delete the relevant section from .git/config
  • 从.git/config中删除相关部分
  • Run git rm --cached path_to_submodule (no trailing slash)
  • 运行git rm——缓存的path_to_submodule(无结尾斜杠)
  • 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.

现在,添加带有——name标志的新子模块。这将给git提供另一个名称,以便在.git/config中对子模块进行引用,以解调以前存在的子模块,您仍然希望在以前的历史中使用该子模块。

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.

你将会得到你所期望的路径的子模块。

#3


8  

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

#4


4  

What fixed this for me was in the root of your git repo (not the submodule), run

对我来说,在git repo(而不是子模块)的根目录中,运行

rm -rf .git/modules/yourmodule

Then you should be able to add as normal.

然后你应该可以像平常一样添加。

#5


2  

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.

我不喜欢手工修改我的.gitmodules。我也写了一篇关于它的博文。

#6


0  

If you want to change the remote URL only for this clone:

如果您只想为这个克隆更改远程URL:

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.

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

This is described as "user specific record changes" here.

这里描述为“用户特定记录更改”。

Do not run git submodule sync as that will reset to the default URL again.

不要运行git子模块同步,因为这会再次重置到默认的URL。

#1


105  

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

如果子模块的位置(URL)发生变化,则可以简单地:

  1. Modify your .gitmodule file to use the new URL
  2. 修改.gitmodule文件以使用新的URL
  3. Run git submodule sync
  4. git子模块同步运行

More complete info can be found elsewhere:

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

#2


32  

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
  • 从.gitmodules文件中删除相关部分
  • Delete the relevant section from .git/config
  • 从.git/config中删除相关部分
  • Run git rm --cached path_to_submodule (no trailing slash)
  • 运行git rm——缓存的path_to_submodule(无结尾斜杠)
  • 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.

现在,添加带有——name标志的新子模块。这将给git提供另一个名称,以便在.git/config中对子模块进行引用,以解调以前存在的子模块,您仍然希望在以前的历史中使用该子模块。

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.

你将会得到你所期望的路径的子模块。

#3


8  

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

#4


4  

What fixed this for me was in the root of your git repo (not the submodule), run

对我来说,在git repo(而不是子模块)的根目录中,运行

rm -rf .git/modules/yourmodule

Then you should be able to add as normal.

然后你应该可以像平常一样添加。

#5


2  

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.

我不喜欢手工修改我的.gitmodules。我也写了一篇关于它的博文。

#6


0  

If you want to change the remote URL only for this clone:

如果您只想为这个克隆更改远程URL:

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.

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

This is described as "user specific record changes" here.

这里描述为“用户特定记录更改”。

Do not run git submodule sync as that will reset to the default URL again.

不要运行git子模块同步,因为这会再次重置到默认的URL。