go get报错:fatal: could not read Username for ‘https://xxx.com‘: terminal prompts disabled

错误触发

我正确配置了自己的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: 这么小的问题也要写文章?小题大作了)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值