最初的.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
?
如果要仅为本地存储修改用于子模块的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
我尝试了上述解决方案,但出于某种原因出现了一堆问题。以下是另一种选择。
在新的存储库中:
git submodule init
git clone ../local/path/to/submodule.git path/in/repo/submodule
手动初始化每个子模块每当根存储库子模块哈希值发生变化时,您需要同步子模块:
git submodule update
可能与--force
和--recursive
有很好的衡量标准您不仅可以使用git config submodule.moduleName.url
更改子模块URL,而且使用Git 2.22(2019年第2季度),您也可以取消设置:
commit b57e811见commit c89c494,commit 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