如何用另一个 repo 替换 git 子模块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14404704/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I replace a git submodule with another repo?
提问by joseph.hainline
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[email protected]:userA/TestFramework.git
- I'd like it to now point to
[email protected]:userB/TestFramework.git
.
- 位于
./ExternalFrameworks/TestFramework
那个指向 git repo[email protected]:userA/TestFramework.git
- 我希望它现在指向
[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.
回答by Tim Henigan
If the location (URL) of the submodule has changed, then you can simply:
如果子模块的位置(URL)发生了变化,那么您可以简单地:
- Modify your
.gitmodule
file to use the new URL - Delete the submodule folder in the repo
rm -rf .git/modules/<submodule>
- Delete the submodule folder in the working directory
rm -rf <submodule>
- Run
git submodule sync
- Run
git submodule update
- 修改您的
.gitmodule
文件以使用新 URL - 删除repo中的子模块文件夹
rm -rf .git/modules/<submodule>
- 删除工作目录下的子模块文件夹
rm -rf <submodule>
- 跑
git submodule sync
- 跑
git submodule update
More complete info can be found elsewhere:
更完整的信息可以在其他地方找到:
回答by joseph.hainline
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
- 从
.gitmodules
文件中删除相关部分 - 删除相关部分
.git/config
- 运行
git rm --cached path_to_submodule
(无斜杠) - 提交并删除现在未跟踪的子模块文件
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 [email protected]:userB/TestFramework.git
and you'll get the submodule loaded at the path you expect.
并且您将在您期望的路径上加载子模块。
回答by Pavan Sokke Nagaraj
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
回答by hobberwickey
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.
然后你应该可以正常添加。
回答by joeytwiddle
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 notrun git submodule sync
as that will reset to the default URL again.
不要运行,git submodule sync
因为这会再次重置为默认 URL。
回答by DerZyklop
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 blogpostabout it.