How to resolve the 'terminal prompts disabled' error when using go get for a private GitHub repository

I’m experiencing an issue when trying to access a private GitHub repository using the go get command. Here’s a bit of background:

I created a private repo named mycompany/private-repo on GitHub via their web interface. On my primary workstation, everything went smoothly:

$ cd $GOPATH
$ go get github.com/mycompany/private-repo

I added a new file called handler.go, committed the changes, and pushed them to the repo successfully:

$ vim handler.go
$ git add handler.go
$ git commit -m "Add handler function"
$ git push origin main

But when I attempted to clone it on a different laptop, I encountered the following error:

$ cd $GOPATH
$ go get github.com/mycompany/private-repo
cd .; git clone https://github.com/mycompany/private-repo /home/user/go/src/github.com/mycompany/private-repo
Cloning into '/home/user/go/src/github.com/mycompany/private-repo'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled
package github.com/mycompany/private-repo: exit status 128

I anticipated a prompt for my GitHub credentials, but it didn’t happen and failed instead. How can I properly authenticate for private repositories using go get?

had the same issue last week! go get can’t prompt for credentials when running non-interactively. you can fix this by setting up a personal access token in your git config, or use git config --global url."[email protected]:".insteadOf "https://github.com/" to force ssh instead of https.