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

abd*_*lam 8 git git-submodules

原始.gitmodules文件使用硬编码的httpsURL但是对于一些自动化测试,我克隆ssh并使子模块URL相对,如../ModuleName.我也不想将这些变化推回到回购中.

# change the https://github.com/ with git@github.com:
sed -i 's/https:\/\/github.com\//git@github.com:/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
Run Code Online (Sandbox Code Playgroud)

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

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

Sco*_*don 21

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

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

git submodule init
Run Code Online (Sandbox Code Playgroud)

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

正如@jthill所指出的,修改子模块URL的一种更简单的方法是:

git config submodule.moduleName.url ssh://user@server/path
Run Code Online (Sandbox Code Playgroud)

在这一点上,你希望运行git submodule sync,因为这将覆盖您刚刚从价值观所做的更改.gitmodules文件.相反,直接去:

git submodule update --recursive --remote
Run Code Online (Sandbox Code Playgroud)