如何在本地更改git子模块url?

问题描述 投票:6回答:3

最初的.gitmodules文件使用硬编码的https网址但是对于一些自动化测试我从ssh克隆并使子模块网址相对于../ModuleName。我也不想将这些变化推回到回购中。

# change the https://github.com/ with [email protected]:
sed -i 's/https:\/\/github.com\//[email protected]:/g' ".git/config"
# delete the url lines from the submodule blocks of .git/config
sed -i '/submodule/ {$!N;d;}' ".git/config"
# change hardcoded https:// urls of submodules to relative ones
sed -i 's/https:\/\/github.com\/ProjName/../g' ".gitmodules"
# also make the same change in the .git/modules/*/config
sed -i 's/https:\/\/github.com\/ProjName/../g' .git/modules/*/config
# sync and update
git submodule sync
git submodule update --init --recursive --remote

通过上面的代码片段,它可以实现我想要的功能。然而,令人讨厌的是,.git/modules/文件夹似乎没有版本控制,但如果我只是删除它,git submodule sync和大多数其他Git操作只是停止工作。

有没有办法在修改.git/modules.gitmodules文件后重新生成.git/config

git git-submodules
3个回答
16
投票

如果要仅为本地存储修改用于子模块的URL,则不要修改.gitmodules文件,即仅用于要推送的更改。

相反,首先初始化本地子模块配置:

git submodule init

然后修改.git/config文件以照常更改子模块URL。 (同样,这里不需要修改.gitmodules,如果这是一个新的克隆你可能还没有.git/modules。)

作为@jthill points out,修改子模块URL的更简单方法是:

git config submodule.moduleName.url ssh://user@server/path

此时您不想运行git submodule sync,因为这将覆盖您刚刚使用.gitmodules文件中的值所做的更改。相反,直接去:

git submodule update --recursive --remote

0
投票

我尝试了上述解决方案,但出于某种原因出现了一堆问题。以下是另一种选择。

在新的存储库中:

  1. git submodule init
  2. 使用git clone ../local/path/to/submodule.git path/in/repo/submodule手动初始化每个子模块

每当根存储库子模块哈希值发生变化时,您需要同步子模块:

  1. 更新根存储库
  2. git submodule update可能与--force--recursive有很好的衡量标准

0
投票

您不仅可以使用git config submodule.moduleName.url更改子模块URL,而且使用Git 2.22(2019年第2季度),您也可以取消设置:

commit b57e811commit c89c494commit 7a4bb55(2019年2月8日)和Denton Liu (Denton-L)(2019年2月7日)。 (由Junio C Hamano -- gitster --合并于commit 01f8d78,2019年4月25日)

submodule--helper:teach config子命令--unset

这教导子模块 - 帮助器配置--unset选项,它从.gitmodule文件中删除指定的配置键。

那是:

git submodule--helper config --unset submodule.submodule.url &&
git submodule--helper config submodule.submodule.url >actual &&
test_must_be_empty actual
© www.soinside.com 2019 - 2024. All rights reserved.