git - 获取私有(private)包裹时无法读取用户名

标签 git go go-modules

我有一个 Go 项目使用托管在 github 中的私有(private)库。目前正在使用 go 模块,所以我通过 SSH 配置了对 github 的访问,这样我就可以使用再次 go get github.com/myorg/mylibrary@commitId:

github.com/myorg/mylibrary@v5.2.1-0.20210110224145-108714b2f88c: verifying module: 
github.com/myorg/mylibrary@v5.2.1-0.20210110224145-108714b2f88c: reading https://sum.golang.org/lookup/github.com/!myorg/mylibrary@v5.2.1-0.20210110224145-108714b2f88c: 410 Gone
        server response:
        not found: github.com/myorg/mylibrary@v5.2.1-0.20210110224145-108714b2f88c: invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* in /tmp/gopath/pkg/mod/cache/vcs/b186106557bfd44bc932dd151f0fcd2758d8d89d09ab32f8915de7f151e393d1: exit status 128:
                fatal: could not read Username for 'https://github.com': terminal prompts disabled 

我已经尝试过:

env GIT_TERMINAL_PROMPT=1 go get github.com/myorg/mylibrary@commitId

但还是一样。那么,为什么要得到这个?我该如何克服它?

最佳答案

go module可以使用ssh+公钥访问github私有(private)项目

首先将git HTTP方案替换为ssh方案

git config -e
// if you want replace scheme with global config use --global
git config --global -e

然后添加下面几行

[url "git@github.com:"]
     insteadOf = https://github.com

确保你的公钥已经添加到github

cat ~/.ssh/id_rsa.pub
// if file not found use ssh-keygen to generate it 
ssh-keygen

添加公钥到github access below URL add it

https://github.com/settings/keys

现在你可以在go mod中获取github私有(private)项目

关于git - 获取私有(private)包裹时无法读取用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66269018/

相关文章:

git - git push 是慢了还是出了什么问题?

git - ssh:无法解析主机名(Docker、gitlab-ce)

git命令将一个文件夹移动到另一个文件夹

go - 如何遍历请求参数

go - `go list` 没有显示最新版本的包怎么办?

git - 从与 GPG 签署的 Atom 提交到 Github

Golang echo包中间件实现

c - 在 C 程序中使用 golang 函数

go - 尝试使用 go 模块在 Go 1.11 中部署 Google 云功能时出错

go - 在 Cobra 命令行工具中,如何为不同的标志使用相同的变量?