我从浏览器中使用Github UI创建了私人回购示例站点/myprivaterepo。

然后我进入我的go目录(在桌面上)并克隆它:

$ cd $GOPATH
$ go get github.com/examplesite/myprivaterepo

到目前为止一切顺利。创建文件调度程序。Go,添加到repo,并推送。

$ vim scheduler.go
$ git add scheduler.go
$ git commit
$ git push

的一切很好。但是当我去一台干净的笔记本电脑并试图克隆回购时,我得到了一个错误:

# Now on laptop, which doesn't yet know about the repo
$ cd $GOPATH
$ go get github.com/examplesite/myprivaterepo
# At this point it should ask for my user ID and password ,right? But it doesn't.
# Instead, this error occurs:
cd .; git clone https://github.com/examplesite/myprivaterepo /Users/tom/go/src/github.com/examplesite/myprivaterepo
Cloning into '/Users/tom/go/src/github.com/examplesite/myprivaterepo'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled
package github.com/examplesite/myprivaterepo: exit status 128

为什么我的笔记本电脑讨厌我自己的回购,我怎样才能让它接受它的命运?谢谢。


当前回答

虽然关于在GitHub中使用SSH身份验证有很多答案,但这个问题与go模块的正确用法有关。为此,卢娜·洛夫古德给出了正确的答案。

重申一下,默认情况下Go会尝试使用公共存储库来下载包。我们需要指定它使用SSH进行私有回购的身份验证。要做到这一点,可以使用以下方法:

go env -w GOPRIVATE=github.com/{your-username}/{your-lib}
.
.
go mod vendor     # this will now work for your private lib

Glob模式也可以工作,因此如果您有多个私有存储库,您可以添加

go env -w GOPRIVATE=github.com/{your-username}/*

编辑:措辞正确。

其他回答

我开始明白,为了节省时间,GitHub有助于下载它的CLI gh

有了它,我使用的auth命令,它使我的生活更容易

gh auth login

如果你只想快速开始工作,然后继续你的工作……

只需导出GIT_TERMINAL_PROMPT=1

$ export GIT_TERMINAL_PROMPT=1
$ go get [whatever]

现在它将提示您为shell会话的其余部分输入一个用户/通道。把它放在你的.profile或安装git中,就像上面提到的那样,这是一个更持久的解决方案。

虽然关于在GitHub中使用SSH身份验证有很多答案,但这个问题与go模块的正确用法有关。为此,卢娜·洛夫古德给出了正确的答案。

重申一下,默认情况下Go会尝试使用公共存储库来下载包。我们需要指定它使用SSH进行私有回购的身份验证。要做到这一点,可以使用以下方法:

go env -w GOPRIVATE=github.com/{your-username}/{your-lib}
.
.
go mod vendor     # this will now work for your private lib

Glob模式也可以工作,因此如果您有多个私有存储库,您可以添加

go env -w GOPRIVATE=github.com/{your-username}/*

编辑:措辞正确。

我试着添加这个命令

export GOPRIVATE = github。中银协(private_reponame) *

这对我很有效

Go get默认禁用“终端提示”。这可以通过设置git的环境变量来改变:

env GIT_TERMINAL_PROMPT=1 go get github.com/examplesite/myprivaterepo