错误触发
我正确配置了自己的ssh秘钥并使用它拉取了gitlab上的仓库,但当我执行go get
的时候出现的报错信息如下:
go: git.xxx.com/repo@version: reading git.xxx.com/repo/go.mod at revision version: git ls-remote -q origin in /home/user/go/pkg/mod/cache/vcs/a859037cd660ba00acc98f35862a9afee7d0d3b1c234e88a831653: exit status 128
fatal: could not read Username for 'https://git.xxx.com': terminal prompts disabled
Confirm the import path was entered correctly.
If this is a private repository, see https://golang.org/doc/faq#git_https for additional information.
触发原因
执行go get
命令时,除了前面解析命令参数、环境准备、解析go.mod
、按顺序检查模块外,有关键一步——在检查到本地和缓存中都没有go.mod
中指定的模块后,会进行远程下载,此时如果go.mod
中有private 私人仓库(公司的gitlab),身份验证就出现了问题,因为在Git没有进行任何 URL 替换配置(如 insteadOf)时,Go 工具最终会默认使用 HTTPS 来处理未明确指定为 SSH 的模块路径。此时,你配置的ssh秘钥无效。
同样那些会进行模块检查和下载的命令也会出问题,比如go mod tidy
, go mod vendor
, go list
……
解决方法
查看你的错误信息中fatal: could not read Username for 'https://git.xxx.com': terminal prompts disabled
的部分,执行
git config --global --add url."git@xxx.com:".insteadOf "https://git.xxx.com"
这样最快最省事
看到这篇文章就要嘲讽一下自己(内心OS: 这么小的问题也要写文章?小题大作了)