Git配置url.insteadOf无法转换git@github.com为git@ssh.github.com问题
问题
我希望在GitHub Action中使用443端口,通过部署密钥安装私有仓库。当包名中使用git@ssh.github.com时,以下代码运行正常:
- name: Try pip install 443 run: | ssh-keyscan -p 443 -t rsa ssh.github.com >> ~/.ssh/known_hosts git config --global core.sshCommand "ssh -p 443" pip install git+ssh://git@ssh.github.com/my-org/my-repo.git@v1.0.0#subdirectory=core-utils
但用户添加依赖时会使用git@github.com而非git@ssh.github.com,而使用443端口的SSH需要转换该地址。我尝试配置git config --global url."ssh://git@ssh.github.com/".insteadOf "ssh://git@github.com/"却未生效,执行对应代码时报错:
kex_exchange_identification: Connection closed by remote host Connection closed by 140.82.113.3 port 443 fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
请问该git配置哪里出错了?
解决方法
你的insteadOf配置只覆盖了带完整ssh://协议前缀的ssh://git@github.com/地址,但用户实际用的git@github.com是SSH简写URL格式(无ssh://前缀),所以当前规则匹配不上。
需要补充针对简写格式的配置,完整命令如下:
git config --global url."ssh://git@ssh.github.com/".insteadOf "ssh://git@github.com/" git config --global url."ssh://git@ssh.github.com/".insteadOf "git@github.com:"
注意两个细节:
- SSH简写格式中,
github.com后面是冒号:而非斜杠/,配置时要严格对应 ssh.github.com是GitHub官方指定的443端口SSH访问域名,连接它默认走443端口,不需要额外通过core.sshCommand指定端口(保留原配置也不会冲突)
调整后,pip解析git+ssh://git@github.com/xxx或git@github.com:xxx格式的依赖时,都会自动替换为ssh://git@ssh.github.com/xxx,配合之前的ssh-keyscan配置就能正常拉取私有仓库代码了。
内容的提问来源于stack exchange,提问作者mindwrapper
相关产品推荐
相关产品推荐

