如何使用SSH密钥免账号密码执行git clone --recurse-submodules克隆GitHub仓库
解决方案
原因分析
问题出在主仓库的.gitmodules配置文件里,子模块的URL大概率用的是HTTPS协议而非SSH协议。虽然你单独克隆子模块时用的是SSH地址,但递归拉取时Git会读取.gitmodules里的配置,走HTTPS就会触发账号密码验证。
解决办法
方法1:修改主仓库的.gitmodules配置(推荐)
- 先正常克隆主仓库(不用递归子模块):
git clone git@github.com:{company_name}/{main_repo}.git cd {main_repo}
- 查看
.gitmodules文件里的子模块URL:
cat .gitmodules
如果看到类似url = https://github.com/{company_name}/{submodule_repo}.git的HTTPS地址,改成SSH格式:
[submodule "submodule_repo"] path = submodule_repo url = git@github.com:{company_name}/{submodule_repo}.git
- 同步子模块的本地配置:
git submodule sync
- 提交修改到主仓库(方便其他协作成员复用):
git add .gitmodules git commit -m "Update submodule URL to SSH" git push
- 现在重新递归克隆主仓库,就不会要求输入密码了:
git clone --recurse-submodules git@github.com:{company_name}/{main_repo}.git
方法2:全局配置Git自动替换HTTPS为SSH(临时/全局生效)
如果不想修改主仓库的配置,可以给Git设置全局规则,让所有GitHub的HTTPS请求自动转为SSH:
git config --global url."git@github.com:".insteadOf "https://github.com/"
设置完成后,不管是递归克隆还是单独拉取子模块,都会自动用SSH协议,无需输入账号密码。
内容的提问来源于stack exchange,提问作者Code True
相关产品推荐
相关产品推荐

