原始 .gitmodules
文件使用硬编码的 https
url 但对于一些自动化测试,我从 ssh
克隆并使子模块 url 相对如 ../ModuleName
中所示。我也不想将这些更改推回 repo 协议(protocol)中。
# 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
通过上面的代码片段,它可以满足我的要求。然而,烦人的是,.git/modules/
文件夹似乎不受版本控制,但如果我只是删除它,git submodule sync
和大多数其他 Git操作停止工作。
有没有办法在修改.gitmodules
和.git/config
文件后重新生成.git/modules
?
最佳答案
如果你只想为本地仓库修改用于子模块的 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 - 如何在本地更改 git 子模块 url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42028437/