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

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

其他回答

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

只需导出GIT_TERMINAL_PROMPT=1

$ export GIT_TERMINAL_PROMPT=1
$ go get [whatever]

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

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

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

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

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

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

我发现这非常有用,它解决了我的问题。这个命令将允许你的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将像这样:

[url "git@github.com:"]
    insteadOf = https://github.com/

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

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

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

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

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