我有一个包含一些子模块的 git 存储库。对于其中一些子模块,我想在我的本地机器上使用不同的远程 URL。根据to the Git Documentation我可以通过覆盖 .git/config
中的 submodule.$name.url
属性来做到这一点。
不幸的是,这个设置似乎根本没有被使用。
重现步骤
git clone http://example.com/parentrepo
.gitmodules
内容现在是:
[submodule "mysubmodule"]
path = mysubmodule
url = https://example.com/mysubmodule
git config submodule.mysubmodule.url http://doesnotexist.com
.git/config
内容现在是:
[submodule "mysubmodule"]
url = http://doesnotexist.com
active = true
之后 git submodule update --remote
成功,而如果它使用覆盖值应该会失败,因为 URL 不存在。
我正在运行 git 版本 2.22.0。
那么为什么不考虑覆盖的 url?
最佳答案
看来@alexei 上面描述的行为是正确的,并且符合 Git 文档; gitsubmodules guide提到.git/modules/<name>/config
优先级高于 .git/config
.
在这种情况下,remote.origin.url
在 .git/modules/<name>/config
优先于 submodule.<name>.url
在 .git/config
.
然而,阿列克谢写道:
Upon init the URL gets saved in
.git/modules/<name>/config
.
事实并非如此;它在初始 git submodule update
上, 或 git submodule update --init
, 或 git clone --recursive
URL 保存在 .git/modules/<name>/config
中(如果子模块克隆成功)。
此外,事后更改 URL 的更简单方法是运行 git config
。在子模块中(不需要像 alexei 写的那样 deinit 和删除子模块 repo):
cd path/to/submodule
git config remote.origin.url http://doesnotexist.com
# or
git -C path/to/submodule config remote.origin.url http://doesnotexist.com
另外,请注意 git submodule sync
将更新两者 .git/config
和 .git/modules/<name>/config
使用 .gitmodules
中记录的值.
关于Git submodule.$name.url 覆盖不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57376342/