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

标签 git git-submodules

原始 .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/

相关文章:

Git:Rebase 术语

git - 使用 only 命令提交子模块中的更改

git - 一次用子分支重新设置分支

gitsosis, redmine, 乘客 : 'not a git repository' error

windows - 我是否需要同时为 Windows 10 和 WSL 安装 Git?

git - 拆分一个 git 存储库以同时处理两个项目

git - 如何设置 .gitmodules 以便贡献者不必 fork 子模块?

git - 是否可以添加/启动一个新的 git 子模块并将忽略选项设置为某个值?

windows - Git 子模块困惑 : how to use git submodules with developers not familiar with git?

git - 在 cygwin 中使用带有 xargs 的子字符串