我有一个 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/