我已经创建了一个带有子模块的git仓库。我可以告诉子模块自己如何更改其远程仓库路径,但是我不知道如何告诉父仓库如何更改子模块的远程仓库路径。
如果我需要手动操作的话,我并不会感到惊讶,因为即使删除子模块也不容易。
我已经创建了一个带有子模块的git仓库。我可以告诉子模块自己如何更改其远程仓库路径,但是我不知道如何告诉父仓库如何更改子模块的远程仓库路径。
如果我需要手动操作的话,我并不会感到惊讶,因为即使删除子模块也不容易。
git submodule sync --recursive
cd <submodule_dir>
git fetch
git checkout origin/master
git branch master -f
git checkout master
git submodule foreach -q git config remote.origin.url
查看“实际”的子模块 URL。 - Joel Purra.git/config
。在运行git submodule sync --recursive
之前,我必须手动更新.gitmodules
和.git/config
,以使我的子模块远程更新。 - desseimgit submodule update --init --recursive --remote
,这实际上会将仓库更改为新的远程地址。 - Jason Axelson使用 Git 2.25(2020年第一季度),您可以修改它。
请参见“ Git 子模块 URL 已更改”和新命令。
git submodule set-url [--] <path> <newurl>
(关于--
分隔符,请参见“双短线作为停止选项解释的信号,将所有后续参数视为字面值”)
警告:Hi-Angel在评论中提到(即使在Git 2.31.1下进行了测试):
One should be careful with
git submodule set-url
because it has a bug:If, inside your
.gitmodules
file, the path looks like thissome-path
, and then you execute a:# Usually causes unexpected behavior git submodule set-url some-path/ new-url
(note the trailing slash
/
insome-path/
), then, instead of modifying existing submodule, the command will add another one. Instead run:# Good git submodule set-url some-path new-url
原始答案(2009年5月,14年前)
实际上,已经在2009年4月提交了一个补丁来澄清gitmodule
的角色。
因此,现在gitmodule文档还没有包括:
位于 Git 工作树顶层目录的 .gitmodules 文件是一个文本文件,其语法与 linkgit:git-config4 的要求相匹配。这基本上证实了 Jim's answer。
git submodule init
"将子模块存储库URL添加到.git/config中。git submodule sync
"已于2008年8月添加,以使URL更改时(特别是如果子模块数量很重要)该任务变得更加容易。module_list "$@" |
while read mode sha1 stage path
do
name=$(module_name "$path")
url=$(git config -f .gitmodules --get submodule."$name".url)
if test -e "$path"/.git
then
(
unset GIT_DIR
cd "$path"
remote=$(get_default_remote)
say "Synchronizing submodule url for '$name'"
git config remote."$remote".url "$url"
)
fi
done
git config remote."$remote".url "$url"
。
注意:
Git 2.40(2023年第一季度)澄清了git config remote.<remote>.url
:
请参见提交记录 d390e08(2023年2月7日),作者为Calvin Wan(CalvinWan0101
)。
(由Junio C Hamano -- gitster
--于提交记录 59397e9中合并,2023年2月15日)
文档
:澄清多个pushurls
与urls
之间的区别已签署:Calvin Wan
在具有多个配置URL的远程仓库中,git remote -v
(man) 显示fetch使用的正确URL。git config remote.<remote>.url
(man) 返回最后定义的URL,而不是第一个。pushurl
。urls-remotes
现在在其手册页面中包括:
<pushurl>
仅用于推送。它是可选的,默认为
<URL>
。向远程推送会影响所有定义的pushurls或所有定义的urls(如果未定义pushurls)。
但是,如果定义了多个URL,则仅从第一个定义的URL获取。
git submodule set-url [--] <path> <newurl>
中的可选双破折号有什么作用? - jeverling[--]
是什么意思?你能举个例子吗? - Harry这些命令将在命令提示符上完成工作,而不会更改本地代码库中的任何文件。
git config --file=.gitmodules submodule.Submod.url https://github.com/username/ABC.git
git config --file=.gitmodules submodule.Submod.branch Development
git submodule sync
git submodule update --init --recursive --remote
请查看博客中的截图:更改GIT子模块的URL /分支以使用同一存储库的其他URL /分支。git add .gitmodules
、git commit -m "修改子模块URL"
以及git push origin master
。 - skulz00简单来说,您只需要编辑.gitmodules文件,然后重新同步和更新:
通过git命令或直接编辑文件:
git config --file=.gitmodules -e
或者只需:
vim .gitmodules
然后重新同步并更新:
git submodule sync
git submodule update --init --recursive --remote
git submodule update --init
对我有效,--remote
似乎将它绑定到远程仓库的 HEAD 上。 - Chaim Eliyah以下是在Windows下,使用git版本1.8.3.msysgit.0的操作步骤:
完成上述所有操作后,一切都处于我所期望的状态。我想其他仓库的用户在更新时也会遇到类似的问题 - 在您的提交消息中解释这些步骤是明智的!
git submodule update
之后,这是唯一有效的方法。按照其他答案的方法并不能更改./git/modules/external
目录中的内容,因此尝试更新仍会导致它拉取错误的URL。 - NtscCobalt只需编辑您的.git/config文件。例如; 如果您有一个"common"子模块,您可以在超级模块中执行以下操作:
git config submodule.common.url /data/my_local_common
git config --file=.gitmodules -e
命令会打开默认编辑器,您可以在其中更新路径。
.gitmodules
文件,使其指向新的子模块URLsupermodule/.gitmodules
中.gitmodules
文件的最新更改)git submodule update --init --remote path-to-submodule
好了!新克隆的超级模块中的子模块已被正确配置!
git submodule set-url [--] <path> <newurl>
。 - VonC