[英]How to change the remote repository for a git submodule?
我创建了一个带有子模块的 git 存储库。 我可以告诉子模块本身更改其远程存储库路径,但我不确定如何告诉父存储库如何更改子模块的远程存储库路径。
如果我有点不走运并且必须手动执行操作,我不会感到惊讶,因为即使删除子模块也不容易。
您应该能够编辑.gitmodules
文件以更新 URL,然后运行git submodule sync --recursive
以将更改反映到超级项目和您的工作副本。
然后您需要转到.git/modules/path_to_submodule
目录并更改其配置文件以更新 git 路径。
如果 repo 历史记录不同,那么您需要手动检出新分支:
git submodule sync --recursive
cd <submodule_dir>
git fetch
git checkout origin/master
git branch master -f
git checkout master
这些命令将在命令提示符下完成工作,而不会更改本地存储库中的任何文件
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 2.25(2020 年第一季度),您可以对其进行修改。
参见“ Git submodule url changed ”和新命令
git submodule set-url [--] <path> <newurl>
警告: Hi-Angel 在评论中提到(即使使用 Git 2.31.1 测试):
应该小心使用
git submodule set-url
因为它有一个错误:如果在你的
.gitmodules
文件中,路径看起来像这样some-path
,然后你执行一个git submodule set-url some-path/ new-url
(注意尾部斜杠/
),那么,而不是修改现有的子模块,该命令将添加另一个。
原始答案(2009 年 5 月,十年前)
实际上, 2009 年 4 月已经提交了一个补丁来澄清gitmodule
作用。
所以现在gitmodule 文档还没有包括:
.gitmodules
文件位于 git 工作树的顶级目录中,是一个文本文件,其语法符合 -of linkgit:git-config 3 的要求。
[新的]:
由于此文件由 Git 管理,因此它会跟踪项目子模块的 + 记录。
此文件中存储的信息用作提示,以准备存储在项目配置文件中的记录的权威版本。
应该对配置文件进行用户特定的记录更改(例如,考虑到由于网络情况而导致子模块 URL 的差异),而要传播的记录更改(例如,由于子模块源的重新定位)应该对此文件进行.
这几乎证实了吉姆的回答。
如果你遵循这个git submodule 教程,你会发现你需要一个“ 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"
简单来说,你只需要编辑 .gitmodules 文件,然后重新同步和更新:
通过 git 命令或直接编辑文件:
git config --file=.gitmodules -e
要不就:
vim .gitmodules
然后重新同步并更新:
git submodule sync
git submodule update --init --recursive --remote
什么对我有用(在 Windows 上,使用 git 版本 1.8.3.msysgit.0):
git submodule init
和git submodule update
完成所有这些之后,一切都处于我期望的状态。 我想存储库的其他用户在更新时也会有类似的痛苦 - 在提交消息中解释这些步骤是明智的!
只需编辑您的.git/config文件。 例如; 如果您有一个“通用”子模块,您可以在超级模块中执行此操作:
git config submodule.common.url /data/my_local_common
git config --file=.gitmodules -e
打开默认编辑器,您可以在其中更新路径
蛮力方法:
.gitmodules
文件以指向新的子模块 url,supermodule/.gitmodules
,.gitmodules
文件的最新更改反映在克隆中),git submodule update --init path-to-submodule
上运行git submodule update --init path-to-submodule
submodule ,等等! 超级模块的新克隆中的子模块配置正确!
很多人(无论是在这里还是在互联网上)都提出了需要手动编辑或删除多个文件的解决方案。 但这真的不需要!
即使在 Git 2.25
(因此git submodule set-url <path> <newurl>
)不可用的环境中,最简单的解决方案是简单地“取消注册”子模块并使用新 URL 再次添加它。
根据 Git 版本和子模块设置,您可能需要在再次添加之前手动删除<path>
。 无需其他手动操作!
git submodule deinit <path>
rm -rf <path>
git submodule add <repository> [<path>]
之后.gitmodules
文件将有一个不同的 URL 并且应该被提交。 所有其他地方(配置、工作树)已经由 git 处理。
为了解释deinit
作用,我想引用 Git 手册中的内容:
deinit [-f|--force] (--all|[--] <path>…)
取消注册给定的子模块,即从 .git/config 中删除整个
submodule.$name
部分及其工作树。 进一步调用 [..] 将跳过任何未注册的子模块,直到它们再次初始化
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.