在 GitHub 私有仓库中使用 `go get` 命令会导致 “terminal prompts disabled” 错误。

huangapple go评论80阅读模式
英文:

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

问题

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

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

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

到目前为止一切顺利。我创建了文件scheduler.go,添加到仓库并推送。

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

一切都好。但是当我拿着一台干净的笔记本电脑尝试克隆该仓库时,出现了错误:

# 现在在笔记本电脑上,它还不知道这个仓库
$ cd $GOPATH
$ go get github.com/examplesite/myprivaterepo
# 这时它应该要求输入我的用户名和密码,对吗?但它没有。
# 相反,出现了这个错误:
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

为什么我的笔记本电脑对我的仓库不友好,我该如何让它接受它的命运?谢谢。

英文:

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

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

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

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

$ 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:

# 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.

答案1

得分: 691

我发现这个非常有帮助,解决了我的问题。这个命令将允许你的2FA正常工作(并且省去了输入用户名和密码的麻烦):

对于Github:

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

对于Gitlab:

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

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

如果你没有使用2FA,你仍然可以使用SSH,这个方法也适用。

生成的.gitconfig文件将会是这样的:

insteadOf = https://github.com/
英文:

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):

For Github:

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

For Gitlab:

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

Source: 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.

Resulting .gitconfig will be like:

insteadOf = https://github.com/

答案2

得分: 254

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

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

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

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

答案3

得分: 158

它抱怨是因为它需要使用ssh而不是https,但你的git仍然配置为使用https。所以基本上,正如其他人之前提到的,你需要启用提示或者配置git使用ssh而不是https。一个简单的方法是运行以下命令:

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

或者,如果你已经在机器上使用sshgit,你可以安全地编辑~/.gitconfig文件,在最底部添加以下行:

**注意:**这适用于所有的SVC(源代码版本控制),具体取决于你使用的是什么,如github、gitlab、bitbucket。

# Enforce SSH
insteadOf = https://github.com/
insteadOf = https://gitlab.com/
insteadOf = https://bitbucket.org/
  • 如果你想禁用密码提示,你需要缓存密码。有关如何在Mac、Windows或Linux上缓存你的GitHub密码的更多信息,请访问此页面

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

另外,更重要的是,如果这是一个公司或个人的私有存储库,你可能需要跳过使用代理或校验数据库来避免将它们公开。

为此,你需要设置GOPRIVATE环境变量,该变量控制go命令认为是私有的模块(不公开可用)并且因此不应使用代理或校验数据库。该变量是一个逗号分隔的模块路径前缀模式列表(与Go的path.Match语法相同)。例如,

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

或者

go env -w GOPRIVATE=github.com/mycompany/*
  • 有关如何解决私有包/模块校验问题的更多信息,请阅读此文章
  • 有关Go 1.13模块和新增功能的更多信息,请查看Go 1.13模块发布说明

最后一件事不容忘记提到的是,你仍然可以配置go get通过https进行身份验证和获取,你只需要将以下行添加到$HOME/.netrc文件中:

machine github.com login USERNAME password APIKEY
  • 对于GitHub帐户,密码可以是个人访问令牌
  • 有关如何执行此操作的更多信息,请查看Go FAQ页面。

如果需要更多支持或帮助,请随时留言。

英文:

It complains because it needs to use ssh instead of https but 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 ssh instead of https. a simple way to do this by running the following:

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

or if you already use ssh with git in your machine, you can safely edit ~/.gitconfig and add the following line at the very bottom

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

# Enforce SSH
insteadOf = https://github.com/
insteadOf = https://gitlab.com/
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.

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 GOPRIVATE environment variable that controls which modules the go command considers to be private (not available publicly) and should therefore NOT use the proxy or checksum database.

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

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.

One last thing not to forget to mention, you can still configure go get to authenticate and fetch over https, all you need to do is to add the following line to $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 FAQ page.

Please feel free to leave a comment in case you want more support or help.

答案4

得分: 48

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

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

  1. 设置osxkeychain凭据助手:https://help.github.com/articles/caching-your-github-password-in-git/

  2. 如果使用两步验证,而不是使用密码,生成一个具有repo范围的个人访问令牌:https://github.com/settings/tokens

  3. 克隆一个私有仓库,只是为了使其缓存密码:git clone https://github.com/user/private_repo,使用你的github.com用户名作为用户名,生成的个人访问令牌作为密码。

  4. 删除刚刚克隆的仓库,并重新测试以确保凭据已被缓存:git clone https://github.com/user/private_repo,这次不会要求输入凭据。

  5. go get将与个人访问令牌可以访问的任何仓库一起工作。你可能需要使用其他帐户/令牌重复这些步骤,因为权限会有所不同。

英文:

1st -- go get will 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.

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.

  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_repo and 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_repo and this time wasnt asked for creds.

  5. 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.

答案5

得分: 26

从Go 1.13版本开始,如果您已经在终端中配置了git凭据,但仍然遇到此问题,那么您可以尝试设置GOPRIVATE环境变量。设置这个环境变量对我解决了这个问题。

export GOPRIVATE=github.com/{包的组织名称/用户名}/*
英文:

From go 1.13 onwards, if you had already configured your terminal with the git credentials and yet facing this issue, then you could try setting the GOPRIVATE environment variable. Setting this environment variable solved this issue for me.

export GOPRIVATE=github.com/{organizationName/userName of the package}/*  

答案6

得分: 23

如果你只是想让go get快速工作,并继续你的工作...

只需导出GIT_TERMINAL_PROMPT=1

$ export GIT_TERMINAL_PROMPT=1
$ go get [whatever]

现在它会提示你输入用户名和密码,以供你在当前的shell会话中使用。将此添加到你的.profile文件中,或者按照上述步骤设置git,以获得更长久的解决方案。

英文:

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

Just export 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 .profile or setup git as above for a more permanent solution.

答案7

得分: 23

将以下内容添加到您的bash_profile文件中:

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

然后运行以下命令:

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

Add this is your bash_profile:

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

Then run:

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

答案8

得分: 5

虽然有很多关于在GitHub上使用SSH身份验证的答案,但问题涉及到正确使用go模块。为此,Luna Lovegood在这里提供了正确的答案链接

再次强调,默认情况下,Go会尝试使用公共存储库来下载包。我们需要指定它使用SSH进行私有存储库的身份验证。为此,可以使用以下命令:

.
.
go mod vendor     # 这将适用于您的私有库

通配符也可以使用,所以如果您有多个私有存储库,可以添加以下命令:

编辑:更正措辞。

英文:

While there are a lot of answers around using SSH authentication for GitHub, the question pertains to the correct usage for go modules. To this end, Luna Lovegood provides the right answer here.

To reiterate, by default Go tries to use a public repository to download packages. We need to specify that it authenticate using SSH for private repos. And to do this, the following can be used:

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

Glob patterns also work, so if you have multiple private repositories, you can add

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

Edit: Correct wording.

答案9

得分: 5

我尝试添加了这个命令:

export GOPRIVATE=github.com/{private_reponame}/*

并且它对我起作用了。

英文:

I tried adding this command

> export GOPRIVATE=github.com/{private_reponame}/*

and it worked for me

答案10

得分: 4

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

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

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

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

并且不要忘记从你的私有仓库生成一个访问令牌。当提示输入密码时,只需粘贴访问令牌。祝你克隆愉快 在 GitHub 私有仓库中使用 `go get` 命令会导致 “terminal prompts disabled” 错误。

英文:

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

git config --global --add url. "Git@github.com". 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 在 GitHub 私有仓库中使用 `go get` 命令会导致 “terminal prompts disabled” 错误。

答案11

得分: 1

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

英文:

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.

答案12

得分: 1

我在更改凭据后遇到了“终端提示已禁用”的错误,Carthage无法更新依赖项并在终端上显示此错误。

我只是简单地按照以下步骤操作,问题就解决了...

  1. 从“钥匙串访问”中删除旧凭据。
  2. 尝试在不同位置克隆相同的存储库。
  3. 它要求设置凭据(用户名+密码)。
  4. 然后Carthage更新正常工作。
英文:

I faced this error terminal prompts disabled after changing my credentials, Carthage failed to update the dependencies and show this error on terminal.

I just did the simple following steps and it's started working...

  1. Remove old credentials from keychain access
  2. Tried to clone the same repo in different location.
  3. It asked for credentials (username + password) to set.
  4. And then Carthage update worked as before.

答案13

得分: 1

请尝试以下命令:

GO111MODULE=on GOSUMDB=off go get github.com/examplesite/myprivaterepo

或者:

go clean -modcache
go get github.com/examplesite/myprivaterepo
英文:

Try this cmd:

GO111MODULE=on GOSUMDB=off go get github.com/examplesite/myprivaterepo

Or:

go clean -modcache
go get github.com/examplesite/myprivaterepo

答案14

得分: 1

如果存储库是私有的并且需要身份验证。您需要传递您的凭据。下面的命令可以帮助您:

 git config --global --add url.https://YOUR_USER_NAME:YOUR_GIT_TOKEN@gitswarm.YOUR_HOST.com.insteadof https://gitswarm.YOUR_HOST.com

创建令牌的文档:

英文:

If the repo is private and needs authentication. Where you need to pass your credentials. The below command would help

 git config --global --add url.https://YOUR_USER_NAME:YOUR_GIT_TOKEN@gitswarm.YOUR_HOST.com.insteadof https://gitswarm.YOUR_HOST.com

DOC to create a token

答案15

得分: 0

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

例如,要访问GitLab上的一个仓库,我使用以下格式的URL:

https://<user>:<access-token>@<gitlab-server>/<path-to-repo>.git

英文:

Another option is to add the credentials as part of the URL.

To access a repository on GitLab, for example, I use this format for the URL:

https://<user>:<access-token>@<gitlab-server>/<path-to-repo>.git

答案16

得分: -1

我了解到,为了节省时间在GitHub上,可以使用命令行工具gh进行下载。

其中有一个auth命令,我使用它可以让我的生活更轻松。

gh auth login
英文:

I get to understand that to save time with GitHub it helps to download it CLI gh

with it there is auth command which i used and it make my life easier

gh auth login

答案17

得分: -3

如果你已经滚动到这一点,这意味着以上的解决方案都不适用于你。尝试这个

> PS:如果你需要进一步的解释,请点赞和评论。

运行 \
git config --global url.ssh://git@github.com/.insteadOf https://github.com/ && \
ssh-keyscan github.com > /etc/ssh/ssh_known_hosts

ENV GOPRIVATE=github.com/you-company/private-repo

工作目录 /code
添加 . .
运行 --mount=type=ssh \
    --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    go mod download -x
英文:

If you have scrolled down until this point, it means that none of the above solutions worked for you. Try this one.

> PS: Like and comment if you need any further explanation.

RUN \
git config --global url.ssh://git@github.com/.insteadOf https://github.com/ && \
ssh-keyscan github.com > /etc/ssh/ssh_known_hosts

ENV GOPRIVATE=github.com/you-company/private-repo

WORKDIR /code
ADD . .
RUN --mount=type=ssh \
    --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.cache/go-build \
    go mod download -x

huangapple
  • 本文由 发表于 2015年8月27日 01:10:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/32232655.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定