git go get results in 'terminal prompts disabled' error for github private repo

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32232655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:30:39  来源:igfitidea点击:

go get results in 'terminal prompts disabled' error for github private repo

gitgo

提问by tomcam

I created the private repo examplesite/myprivaterepo using the Github UI from my browser.

我使用浏览器中的 Github UI 创建了私有仓库 examplesite/myprivaterepo。

Then I went to my go directory (on the desktop) and cloned it:

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

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

So far so good. Created the file scheduler.go, added to repo, and pushed.

到现在为止还挺好。创建文件 scheduler.go,添加到 repo 并推送。

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

Everythng's OK. But when I went to a clean laptop and tried to clone the repo, I got an error:

一切正常。但是当我找到一台干净的笔记本电脑并尝试克隆 repo 时,出现错误:

# 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

Why is my laptop hating on my own repo and how can I get it to accept its fate? Thanks.

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

采纳答案by Shafreeck Sea

go get disables the "terminal prompt" by default. This can be changed by setting an environment variable of git:

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

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

回答by Wylliam Judd

I found this extremely helpful, and it solved my problem. This command will allow your 2FA to do its thing (and save you the trouble of entering your username and password):

我发现这非常有帮助,它解决了我的问题。此命令将允许您的 2FA 执行其操作(并省去您输入用户名和密码的麻烦):

git config --global --add url."[email protected]:".insteadOf "https://github.com/"

Source: http://albertech.blogspot.com/2016/11/fix-git-error-could-not-read-username.html

来源:http: //albertech.blogspot.com/2016/11/fix-git-error-could-not-read-username.html

If you're not using 2FA, you can still use SSH and this will work.

如果您不使用 2FA,您仍然可以使用 SSH,这将起作用。

Edit: added the --addflag as suggested by slatunje.

编辑:添加了slatunje--add建议的标志

回答by Mike Graf

1st -- go getwill refuse to authenticate on the command line. So you need to cache the credentials in git. Because I use osx I can use osxkeychain credential helper.

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

2nd For me, I have 2FA enabled and thus could not use my password to auth. Instead I had to generate a personal access token to use in place of the password.

第二对我来说,我启用了 2FA,因此无法使用我的密码进行身份验证。相反,我必须生成一个个人访问令牌来代替密码。

  1. setup osxkeychain credential helper https://help.github.com/articles/caching-your-github-password-in-git/
  2. If using TFA instead of using your password, generate a personal access token with repo scope https://github.com/settings/tokens
  3. git clone a private repo just to make it cache the password git clone https://github.com/user/private_repoand used your github.com username for username and the generated personal access token for password.
  4. Removed the just cloned repo and retest to ensure creds were cached -- git clone https://github.com/user/private_repoand this time wasnt asked for creds.

    1. 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.
  1. 设置 osxkeychain 凭证助手https://help.github.com/articles/caching-your-github-password-in-git/
  2. 如果使用 TFA 而不是使用您的密码,请生成具有 repo 范围https://github.com/settings/tokens的个人访问令牌
  3. git clone 一个私有git clone https://github.com/user/private_repo仓库只是为了让它缓存密码 并使用你的 github.com 用户名作为用户名和生成的个人访问令牌作为密码。
  4. 删除了刚刚克隆的 repo 并重新测试以确保 creds 被缓存 -git clone https://github.com/user/private_repo这次没有要求 creds。

    1. go get 将与个人访问令牌可以访问的任何存储库一起使用。由于权限不同,您可能需要对其他帐户/令牌重复这些步骤。

回答by EdH

If you just want go getto work real fast, and move along with your work...

如果您只是想go get真正快速地工作,并随着您的工作而移动……

Just export GIT_TERMINAL_PROMPT=1

只是出口 GIT_TERMINAL_PROMPT=1

$ export GIT_TERMINAL_PROMPT=1
$ go get [whatever]

It will now prompt you for a user/pass for the rest of your shell session. Put this in your .profileor setup gitas above for a more permanent solution.

现在,它会提示您为其余的 shell 会话输入用户/密码。把它放在你的.profilegit上面的设置中以获得更持久的解决方案。

回答by Muhammad Soliman

It complains because it needs to use sshinstead of httpsbut your git is still configured with https. so basically as others mentioned previously you need to either enable prompts or to configure git to use sshinstead of https. a simple way to do this by running the following:

它抱怨是因为它需要使用ssh而不是,https但你的 git 仍然配置了 https。所以基本上就像前面提到的其他人一样,您需要启用提示或配置 git 来ssh代替.git 使用https。通过运行以下命令来执行此操作的简单方法:

git config --global --add url."[email protected]:".insteadOf "https://github.com/"

or if you already use sshwith gitin your machine, you can safely edit ~/.gitconfigand add the following line at the very bottom

或者,如果您已经在您的机器中使用sshwith git,您可以安全地编辑~/.gitconfig并在最底部添加以下行

Note:This covers all SVC, source version control, that depends on what you exactly use, github, gitlab, bitbucket)

注意:这涵盖了所有 SVC、源版本控制,这取决于你到底使用什么,github、gitlab、bitbucket)

# Enforce SSH
[url "ssh://[email protected]/"]
  insteadOf = https://github.com/
[url "ssh://[email protected]/"]
        insteadOf = https://gitlab.com/
[url "ssh://[email protected]/"]
  insteadOf = https://bitbucket.org/
  • If you want to keep password pompts disabled, you need to cache password. For more information on how to cache your github password on mac, windows or linux, please visit this page.

  • For more information on how to add ssh to your github account, please visit this page.

  • 如果要禁用密码提示,则需要缓存密码。有关如何在 mac、windows 或 linux 上缓存您的 github 密码的更多信息,请访问此页面

  • 有关如何将 ssh 添加到您的 github 帐户的更多信息,请访问此页面

Also, more importantly, if this is a private repository for a company or for your self, you may need to skip using proxy or checksum database for such repos to avoid exposing them publicly.

此外,更重要的是,如果这是公司或您自己的私有存储库,您可能需要跳过对此类存储库使用代理或校验和数据库,以避免公开它们。

To do this, you need to set GOPRIVATEenvironment variable that controls which modules the go command considers to be private (not available publicly) and should therefore NOTuse the proxy or checksum database.

为此,您需要设置GOPRIVATE环境变量来控制 go 命令认为哪些模块是私有的(不公开),因此不应使用代理或校验和数据库。

The variable is a comma-separated list of patterns (same syntax of Go's path.Match) of module path prefixes. For example,

该变量是以逗号分隔path.Match的模块路径前缀模式列表(与 Go 的语法相同)。例如,

export GOPRIVATE=*.corp.example.com,github.com/mycompany/*

Or

或者

go env -w GOPRIVATE=github.com/mycompany/*
  • For more information on how to solve private packages/modules checksum validation issues, please read this article.
  • For more information about go 13 modules and new enhancements, please check out Go 1.13 Modules Release notes.
  • 有关如何解决私有包/模块校验和验证问题的更多信息,请阅读本文
  • 有关 go 13 模块和新增强功能的更多信息,请查看Go 1.13 模块发行说明

One last thing not to forget to mention, you can still configure go getto authenticate and fetch over https, all you need to do is to add the following line to $HOME/.netrc

最后一件事不要忘记提到,您仍然可以配置go get为身份验证和获取https,您需要做的就是添加以下行$HOME/.netrc

machine github.com login USERNAME password APIKEY
  • For GitHub accounts, the password can be a personal access tokens.
  • For more information on how to do this, please check Go FAQpage.
  • 对于 GitHub 帐户,密码可以是个人访问令牌
  • 有关如何执行此操作的更多信息,请查看 Go FAQ页面。

I hope this helps the community and saves others' time to solve described issues quickly. please feel free to leave a comment in case you want more support or help.

我希望这有助于社区并节省其他人的时间来快速解决所描述的问题。如果您需要更多支持或帮助,请随时发表评论。

回答by cooljl86

If you configure your gitconfig with this option, you will later have a problem cloning other repos of GitHub

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

git config --global --add url. "[email protected]". Instead, "https://github.com/"

Instead, I recommend that you use this option

相反,我建议您使用此选项

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

and do not forget to generate an access token from your private repository. When prompted to enter your password, just paste the access token. Happy clone :)

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

回答by Nick Proto

I had the same problem on windows "error: failed to execute prompt script (exit code 1) fatal: could not read Username for 'https://github.com': No error" trying to login to github through the login dialog. When I canceled the dialog git asked me for login and password in the command line and it worked fine.

我在 Windows 上遇到了同样的问题“错误:无法执行提示脚本(退出代码 1)致命:无法读取用户名‘ https://github.com’:没有错误”试图通过登录对话框登录到 github。当我取消对话框时,git 要求我在命令行中输入登录名和密码,并且运行良好。