如何在本地更改 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 12:35:35  来源:igfitidea点击:

How to change git submodules url locally?

gitgit-submodules

提问by abdus_salam

The original .gitmodulesfile uses the hard coded httpsurls but for some automated tests I clone from sshand 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 syncand most other Git operations just stop working.

使用上面的代码片段,它可以满足我的要求。然而,令人讨厌的是,.git/modules/文件夹似乎不受版本控制,但如果我只是删除它,git submodule sync大多数其他 Git 操作就会停止工作。

Is there a way to get the .git/modulesregenerated after modifying the .gitmodulesand .git/configfiles?

有没有办法.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 .gitmodulesfile, 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/configfile to change the submodule URL as usual. (Again, no need to modify .gitmoduleshere, and if this is a new clone you probably won't have .git/modulesyet.)

然后.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 .gitmodulesfile. 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 command git 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 gitsterHamano合并-- --在2019 年 4 月 25 日提交 01f8d78 中

submodule--helper: teach config subcommand --unset

This teaches submodule--helper configthe --unsetoption, which removes the specified configuration key from the .gitmodulefile.

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 C

Signed-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:

在新的存储库中:

  1. git submodule init
  2. Initialize each submodule manually with git clone ../local/path/to/submodule.git path/in/repo/submodule
  1. git submodule init
  2. 手动初始化每个子模块 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:

每当根存储库子模块哈希更改并且您需要同步子模块时:

  1. Update the root repository
  2. git submodule updatewith maybe --forceand --recursivefor good measure
  1. 更新根存储库
  2. git submodule update有可能--force并且--recursive很好地衡量