Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Git子模块。$name.url重写无效_Git - Fatal编程技术网

Git子模块。$name.url重写无效

Git子模块。$name.url重写无效,git,Git,我有一个包含一些子模块的git存储库。对于其中一些子模块,我希望在本地计算机上使用不同的远程URL。根据需要,我可以通过覆盖我的.git/config中的子模块$name.url属性来实现这一点 不幸的是,似乎根本没有使用此设置 复制步骤 git克隆http://example.com/parentrepo .gitmodules内容现在是: [submodule "mysubmodule"] path = mysubmodule url = https://example.com/mysu

我有一个包含一些子模块的git存储库。对于其中一些子模块,我希望在本地计算机上使用不同的远程URL。根据需要,我可以通过覆盖我的
.git/config
中的
子模块$name.url
属性来实现这一点

不幸的是,似乎根本没有使用此设置

复制步骤

git克隆http://example.com/parentrepo

.gitmodules
内容现在是:

[submodule "mysubmodule"]
 path = mysubmodule
 url = https://example.com/mysubmodule
[submodule "mysubmodule"]
    url = http://doesnotexist.com
    active = true
git config submodule.mysubmodule.urlhttp://doesnotexist.com

.git/config
内容现在是:

[submodule "mysubmodule"]
 path = mysubmodule
 url = https://example.com/mysubmodule
[submodule "mysubmodule"]
    url = http://doesnotexist.com
    active = true
之后,
git子模块更新--remote
成功,而如果它将使用覆盖值,则应该失败,因为URL不存在

我正在运行git版本2.22.0


那么,为什么不考虑被覆盖的url呢?

它看起来像是
子模块..url
属性只会影响
init
命令,并且只会影响以前未创建的模块(
.git/modules/
不存在)

init
时,URL保存在
.git/modules//config
中。
update
命令从那里使用URL。即使
deinit
模块,设置属性,然后重新
init
模块,
.git/modules//config
中的URL将保持不变,因此
update
仍将从“旧”远程URL获取。我发现要真正更改URL,您需要:

git submodule deinit <name>
rm -r .git/modules/<name>/
git config submodule.<name>.url
git init <name>
git sumodule update --remote
git子模块deinit
rm-r.git/modules//
git config子模块..url
git初始化
git sumodule更新--远程

关于您在
.gitmodules
中更改URL的评论:您需要运行
git submodule sync
来应用更改。

上面@alexei描述的行为似乎是正确的,并且符合git文档;提到
.git/modules//config
的优先级高于
.git/config

在这种情况下,
.git/modules//config
中的
remote.origin.url
优于
.git/config
中的
子模块.url

然而,阿列克谢写道:

初始化时,URL保存在
.git/modules//config

事实并非如此;在初始
git子模块更新
、或
git子模块更新--init
、或
git克隆--recursive
时,URL保存在
.git/modules//config
中(如果子模块克隆成功)

此外,事后更改URL的一种更简单的方法是在子模块中运行
git config
(无需像alexei所写的那样去初始化和删除子模块repo):

cd路径/到/子模块
git config remote.origin.urlhttp://doesnotexist.com
#或
git-C path/to/submodule config remote.origin.urlhttp://doesnotexist.com

另外,请注意,
git submodule sync
将使用
中记录的值更新
.git/config
.git/modules//config
,可能
git submodule
足够聪明,可以看到
mysubmodule
位于正确的提交散列中,因此,它不需要执行git pull
…?如何在不联系远程URL的情况下查看并获取信息?如果我在
.gitmodules
中更改URL,它也会中断。我怀疑您最初运行了
git clone--recursive submodules
,或者在运行
git config submodule.mysubmodule.URL
之前运行了
git submodule update--init
。这两种情况都是这样吗?对于我的最小测试场景,没有一种是这样的。我运行了
git init.
,然后
git子模块添加https://example.com/mysubmodule
。你的问题的目的是什么?