如何在本地更改git子模块的URL? [英] How to change git submodules url locally?
问题描述
原始的.gitmodules
文件使用硬编码的https
网址,但是对于某些自动化测试,我从ssh
克隆并像../ModuleName
那样使子模块网址相对.我也不想将这些更改推回存储库中.
The original .gitmodules
file uses the hard coded https
urls but for some automated tests I clone from ssh
and make the submodule urls relative as in ../ModuleName
. I don't want to push these changes back into the repo either.
# 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操作将停止工作.
With the snippet above, it does what I want. However, the annoying thing is, .git/modules/
folder doesn't seem to be under version control but if I just delete it, git submodule sync
and most other Git operations just stop working.
在修改.gitmodules
和.git/config
文件之后,是否有办法让.git/modules
重新生成?
Is there a way to get the .git/modules
regenerated after modifying the .gitmodules
and .git/config
files?
推荐答案
如果只想修改用于本地存储库的子模块的URL,则不要修改.gitmodules
文件,仅用于更改您要推送的内容.
If you want to modify the URL used for a submodule only for the local repo, then don't modify the .gitmodules
file, that is only for changes that you want to push.
相反,首先初始化本地子模块配置:
Instead, first initialize the local submodule config:
git submodule init
然后修改.git/config
文件,以照常更改子模块URL. (再次,无需在此处修改.gitmodules
,如果这是一个新克隆,您可能还没有.git/modules
.)
Then modify the .git/config
file to change the submodule URL as usual. (Again, no need to modify .gitmodules
here, and if this is a new clone you probably won't have .git/modules
yet.)
@jthill指出 ,修改子模块网址的一种简便方法是:
As @jthill points out, an easier way to modify the submodule URLs is:
git config submodule.moduleName.url ssh://user@server/path
此时,您不不想运行git submodule sync
,因为这将覆盖您使用.gitmodules
文件中的值所做的更改.相反,直接去:
At this point you don't want to run git submodule sync
, because that will overwrite the changes you just made with the values from the .gitmodules
file. Instead, go straight to:
git submodule update --recursive --remote
这篇关于如何在本地更改git子模块的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!