我从浏览器中使用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

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


当前回答

如果你用这个选项配置你的gitconfig,你以后会在克隆GitHub的其他repo时遇到问题

git config --global --add url. "Git@github.com". Instead, "https://github.com/"

相反,我建议您使用这个选项

echo "export GIT_TERMINAL_PROMPT=1" >> ~/.bashrc || ~/.zshrc

不要忘记从您的私有存储库生成一个访问令牌。当提示输入密码时,只需粘贴访问令牌。快乐克隆:)

其他回答

另一种选择是将凭证作为URL的一部分添加。

例如,要访问GitLab上的存储库,我使用这样的URL格式:

https:// <用户名>:<访问令牌> @ < gitlab-server > / < path-to-repo > .

这是你的bash_profile:

export GOPRIVATE=github.com/your-organization/*

然后运行:

git config --global --add url."git@github.com:".insteadOf "https://github.com/"

首先,go get将拒绝在命令行上进行身份验证。所以你需要在git中缓存凭证。因为我使用osx,我可以使用osxkeychain凭据助手。

对我来说,我已经启用了2FA,因此不能使用我的密码进行认证。相反,我必须生成一个个人访问令牌来代替密码。

setup osxkeychain credential helper https://help.github.com/articles/caching-your-github-password-in-git/ If using TFA instead of using your password, generate a personal access token with repo scope https://github.com/settings/tokens git clone a private repo just to make it cache the password git clone https://github.com/user/private_repo and used your github.com username for username and the generated personal access token for password. Removed the just cloned repo and retest to ensure creds were cached -- git clone https://github.com/user/private_repo and this time wasnt asked for creds. go get will work with any repos that the personal access token can access. You may have to repeat the steps with other accounts / tokens as permissions vary.

虽然关于在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}/*

编辑:措辞正确。

我在windows上有同样的问题“错误:未能执行提示脚本(退出代码1)” 致命:无法读取用户名“https://github.com':没有错误”试图登录到github通过登录对话框。当我取消对话框时,git在命令行中询问我登录名和密码,它工作得很好。