如何在本地更改 git 子模块 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42028437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change git submodules url locally?
提问by abdus_salam
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.
原来的.gitmodules
文件使用硬编码的https
网址,但对于一些自动化测试,我从克隆ssh
,并相对于在子模块的URL ../ModuleName
。我也不想将这些更改推回 repo。
# 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
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.
使用上面的代码片段,它可以满足我的要求。然而,令人讨厌的是,.git/modules/
文件夹似乎不受版本控制,但如果我只是删除它,git submodule sync
大多数其他 Git 操作就会停止工作。
Is there a way to get the .git/modules
regenerated after modifying the .gitmodules
and .git/config
files?
有没有办法.git/modules
在修改.gitmodules
和.git/config
文件后重新生成?
回答by Scott Weldon
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.
如果您只想修改用于本地存储库的子模块的 URL,则不要修改该.gitmodules
文件,这仅适用于您要推送的更改。
Instead, first initialize the local submodule config:
相反,首先初始化本地子模块配置:
git submodule init
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.)
然后.git/config
像往常一样修改文件以更改子模块 URL。(同样,.gitmodules
这里不需要修改,如果这是一个新的克隆,你可能还.git/modules
没有。)
As @jthill points out, an easier way to modify the submodule URLs is:
正如@jthill 指出的那样,修改子模块 URL 的一种更简单的方法是:
git config submodule.moduleName.url ssh://user@server/path
At this point you don'twant 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 sync
,因为这将覆盖您刚刚从价值观所做的更改.gitmodules
文件。相反,直接转到:
git submodule update --recursive --remote
回答by VonC
Not only you can change the submodule URL with git config submodule.moduleName.url
, but:
您不仅可以使用 更改子模块 URL git config submodule.moduleName.url
,还可以:
With Git 2.25 (Q1 2020), you can modifyit.
See "Git submodule url changed" and the new commandgit submodule set-url [--] <path> <newurl>
with Git 2.22 (Q2 2019), you can also unsetit:
使用 Git 2.25(2020 年第一季度),您可以对其进行修改。
参见“ Git submodule url changed”和新命令git submodule set-url [--] <path> <newurl>
使用 Git 2.22(2019 年第二季度),您还可以取消设置:
See commit b57e811, commit c89c494(08 Feb 2019), and commit 7a4bb55(07 Feb 2019) by Denton Liu (Denton-L
).
(Merged by Junio C Hamano -- gitster
--in commit 01f8d78, 25 Apr 2019)
请参阅提交 b57e811、提交 c89c494(2019 年 2 月 8 日)和提交 7a4bb55(2019 年 2 月 7 日)由Denton Liu ( Denton-L
)提交。
(由Junio C gitster
Hamano合并-- --在2019 年 4 月 25 日提交 01f8d78 中)
submodule--helper
: teach config subcommand--unset
This teaches
submodule--helper config
the--unset
option, which removes the specified configuration key from the.gitmodule
file.
submodule--helper
: 教学配置子命令--unset
这教导
submodule--helper config
了--unset
从.gitmodule
文件中删除指定配置密钥的选项。
That is:
那是:
git submodule--helper config --unset submodule.submodule.url &&
git submodule--helper config submodule.submodule.url >actual &&
test_must_be_empty actual
With Git 2.27 (Q2 2020), the rewriting various parts of "git submodule
" in C continues, include set-url
.
在 Git 2.27(2020 年第二季度)中,git submodule
继续用 C重写“ ”的各个部分,包括set-url
.
See commit 6417cf9(08 May 2020) by Shourya Shukla (periperidip
).
(Merged by Junio C Hamano -- gitster
--in commit 9e8ed17, 13 May 2020)
请参阅Shourya Shukla ( )提交的 6417cf9(2020 年 5 月 8 日)。(由Junio C Hamano合并-- --在提交 9e8ed17 中,2020 年 5 月 13 日)periperidip
gitster
submodule
: port subcommand 'set-url' from shell to CSigned-off-by: Shourya Shukla
Convert submodule subcommand 'set-url' to a builtin. Port '
set-url
' to 'submodule--helper.c
' and call the latter via 'git submodule.sh
'.
submodule
: 从 shell 到 C 的端口子命令“set-url”签字人:Shourya Shukla
将子模块子命令“set-url”转换为内置命令。端口 '
set-url
' 到 'submodule--helper.c
' 并通过 'git submodule.sh
'调用后者。
回答by rendaw
I tried the above solution but had a mess of problems for some reason. The following is an alternative.
我尝试了上述解决方案,但由于某种原因遇到了一堆问题。下面是一个替代方案。
In a new repository:
在新的存储库中:
git submodule init
- Initialize each submodule manually with
git clone ../local/path/to/submodule.git path/in/repo/submodule
git submodule init
- 手动初始化每个子模块
git clone ../local/path/to/submodule.git path/in/repo/submodule
Whenever the root repository submodule hashes change and you need to sync the submodules:
每当根存储库子模块哈希更改并且您需要同步子模块时:
- Update the root repository
git submodule update
with maybe--force
and--recursive
for good measure
- 更新根存储库
git submodule update
有可能--force
并且--recursive
很好地衡量