403

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.

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
tomcam
  • 4,625
  • 3
  • 19
  • 22
  • 11
    This isn't a `go` error, `git` is returning "terminal prompts disabled". Can you run `git clone https://github.com/examplesite/myprivaterepo` on your laptop? – JimB Aug 26 '15 at 17:20
  • 3
    Thank you. The git clone worked. Then the go get worked. What's the deal? Do I have to "prime the pump" like this every time or did I misconfigure something? – tomcam Aug 26 '15 at 22:08
  • 2
    @tomcam, you can add ssh keys to github account and go get will work from the start, see https://help.github.com/articles/generating-ssh-keys/ – alex vasi Aug 27 '15 at 14:13
  • @alexvasi: that only works if he clones the repo himself over ssh first, since it will request from `https://github.com/` if the import path doesn't exist. The git process should be able to prompt for the username/password, so there must be something different in the laptop configuration -- different shell, different .gitconfig, an env variable, etc. – JimB Aug 27 '15 at 17:55
  • All of this has helped me, but I'm not sure how to mark as answered and/or resolved. Here's what I have so far. – tomcam Aug 27 '15 at 20:24
  • (Continued) Not sure how to mark as answered and/or resolved because I am still not sure what the most robust solution is. Here's what I have so far. @JimB suggested I use `git clone https://github.com/examplesite/myprivaterepo` and that worked, but I dont' quite understand why. @alexvasi mentioned ssh keys. I have one already--but that's for my other Github account. I created a new ssh key & file: `ssh-keygen -t rsa -C "myemail@example.com" -f id_rsa_myuser_name` Then copied that key into my private Github account. Maybe the best solution? – tomcam Aug 27 '15 at 20:34
  • I usually work on Windows and I was having this problem. I added the "git@github.com:".insteadOf "https://github.com/" line to .gitconfig and SET the GIT_TERMINAL_PROMPT variable to 1 but that did not work. Then I tried to import the package from my linux subsystem and it ran with no issues. – Sergio Prats Apr 18 '23 at 10:50

17 Answers17

702

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:

[url "git@github.com:"]
    insteadOf = https://github.com/
Vitaly Zdanevich
  • 13,032
  • 8
  • 47
  • 81
Wylliam Judd
  • 9,935
  • 2
  • 26
  • 36
  • 30
    I recommend adding **--add** flag so that you do not overwrite existing settings you may have For example: `git config --global --add url."git@github.com:".insteadOf "https://github.com/"` – slatunje Aug 11 '17 at 11:19
  • Thanks slatunje, added the flag to my answer based on your feedback. – Wylliam Judd Oct 10 '17 at 19:34
  • 7
    This is the best solution IMO. It's more secure and less effort (once setup) to use SSH keys for auth instead of username/password – bodecker Oct 17 '17 at 07:42
  • 11
    Don't do this on Windows. I ended up with error `git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.`. – Shital Shah May 03 '18 at 04:39
  • 1
    works for linux too. should work for everyone if you're using ssh globaly – Chiptus Nov 19 '18 at 12:09
  • 2
    this is great until your company blocks all ssh outbound transport. – krystan honour Oct 07 '19 at 13:58
  • 1
    I use bitbucket, so this command helped me: `git config --global --add url."git@bitbucket.org:".insteadOf "https://bitbucket.org/"` – Andrey Klochkov Apr 13 '20 at 15:10
  • 3
    I'm using windows and now I'm stuck(should spot the warning by @ShitalShah earlier...) any way to undo this? – Circle Hsiao Dec 06 '20 at 16:21
  • 10
    So for whoever wondering how to undo this, it will add a section in `C:\Users\\.gitconfig` which you can remove directly – Circle Hsiao Dec 07 '20 at 16:23
  • Worked as expected, but then I had an issue with Xcode being unable to download swift packages. I had to revert this to make it work. – Gustavo Conde Oct 23 '21 at 08:37
  • 1
    This will help, BUT. As stated in the release doc of Go 1.13 you should [declare GOPRIVATE variable](https://medium.com/mabar/today-i-learned-fix-go-get-private-repository-return-error-reading-sum-golang-org-lookup-93058a058dd8) when go getting private repositories. – redCuckoo Dec 27 '21 at 14:26
  • @CircleHsiao for undo you can edit your .gitconfig – Vitaly Zdanevich Apr 29 '22 at 14:48
  • 1
    I've executed the above, my ~/.gitconfig was updated, but I am still getting `fatal: could not read Username for 'https://github.com': terminal prompts disabled` when `go get`-ing a private repo .. – kev Jun 27 '22 at 09:51
  • I wonder why we need to use the flag --global here? I tried without it and nothing changed but after added it, things worked like a charm. – Joey Dec 03 '22 at 14:51
  • i needed ssh to make this work. Like this: --add url."ssh://git@github.com:" – Tom Seelbach Dec 20 '22 at 12:51
254

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
tantrix
  • 1,248
  • 12
  • 14
Shafreeck Sea
  • 2,673
  • 1
  • 10
  • 6
  • 5
    I've tried all of the top answers here (set environment variable above, update git config using insteadOf, running git clone before go get) and go get is still failing with that issue. Anything else i can try? – user3125693 Nov 05 '20 at 21:54
  • 38
    This did the trick: go env -w GOPRIVATE=github.com/{your-github-user-here}/* – Ferrarezi Dec 25 '20 at 03:34
  • If it helps anyone I copied the `go.mod` and `go.sum` files from another repo and tried to use those. This gave me git login prompts whenever I tried to go `go build` because git kept trying to authenticate to someone else's github repo. To fix this you can run `go mod init` and then you should stop seeing these login prompts. – fIwJlxSzApHEZIl Jan 19 '21 at 02:10
  • 3
    Thanks @Ferrarezi, that was exactly what I needed. https://www.goproxy.io/docs/GOPRIVATE-env.html explains it. – keni Aug 13 '21 at 14:58
159

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
[url "ssh://git@github.com/"]
  insteadOf = https://github.com/
[url "ssh://git@gitlab.com/"]
        insteadOf = https://gitlab.com/
[url "ssh://git@bitbucket.org/"]
  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.

starball
  • 20,030
  • 7
  • 43
  • 238
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
  • 29
    `GOPRIVATE` (`go env -w GOPRIVATE=github.com/mycompany/*`) is the only thing that worked for me. I might suggest putting that under its own heading or even in an answer of its own to highlight it a bit more. – Bernhard Barker Jun 05 '20 at 10:26
  • 9
    For me as well , only `go env -w GOPRIVATE=gitlab.companyname.com` helped. This should be an accepted answer. Thank you for this thorough answer. – Pavel Gordon Aug 13 '20 at 09:08
  • 2
    This should be the accepted answer being thorough. It's saved me on a deadline. – Osoro Mar 03 '22 at 10:34
  • 7
    `export GOPRIVATE=github.com/MyCompanyName/*` is what worked for me. – bit Jul 05 '22 at 12:20
  • export GOPRIVATE=github.com/MyCompanyName/* the only solution here as well, up – MansouriAla Oct 18 '22 at 21:12
  • One very important point regarding the `$HOME/.netrc` file is that its persmission shall be `600`. – Soumya Kanti Apr 30 '23 at 03:57
48

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.

    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.
Mike Graf
  • 5,077
  • 4
  • 45
  • 58
  • 2
    I have 2FA and this worked for me -- generated a token (with full privs), did a `git clone` of one of my own existing repos (it is public actually), was prompted for my `username`, put in my GitHub username, then put in the generated token for `password` -- deleted the cloned repo, then reran the `go get` command and all was well - thanks! (OSX here as well) – Eric Dorsey Jun 02 '16 at 04:35
  • 3
    This is also really great because you can limit the scope of the token to only "read" for private repos so even if it somehow gets into an image in the wild, they can only read your code and not push things into it and you can revoke the token really easily. I can't recall if they have auto expiration built in yet. – dragon788 Oct 18 '17 at 17:30
  • Worked for me, thanks! Just make sure you have the right permissions when you make the key - I just checked the section for `repo` – Alaska Mar 29 '23 at 08:31
26

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}/*  
Surya
  • 2,429
  • 1
  • 21
  • 42
23

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.

EdH
  • 3,194
  • 3
  • 21
  • 23
23

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/"
Vikram Biwal
  • 2,618
  • 1
  • 25
  • 36
6

I tried adding this command

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

and it worked for me

Shriram
  • 123
  • 2
  • 5
  • 1
    I tried all the above options but this one works like a charm. Here is more documentation about it: https://medium.com/mabar/today-i-learned-fix-go-get-private-repository-return-error-reading-sum-golang-org-lookup-93058a058dd8 – dmigwi Sep 07 '22 at 23:26
5

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.

  • I don't think that your last statement is a "regex". It looks like a "glob pattern". Which one is correct? (i.e. if a regex, you probably missed a period, if a pattern, then your statement is incorrect.) – Alexis Wilke Feb 09 '22 at 23:21
  • You are correct @AlexisWilke. What I meant was glob pattern. – Annanay Agarwal Feb 24 '22 at 10:57
4

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

cooljl31
  • 337
  • 2
  • 4
1

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.

Nick Proto
  • 121
  • 1
  • 6
1

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.
RJ168
  • 1,006
  • 2
  • 12
  • 22
1

Try this cmd:

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

Or:

go clean -modcache
go get github.com/examplesite/myprivaterepo
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
1

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

venkat7668
  • 2,657
  • 1
  • 22
  • 26
0

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

Gonen
  • 4,005
  • 1
  • 31
  • 39
-1

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
Mor Bargig
  • 198
  • 1
  • 7
-3

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
Shady Smaoui
  • 867
  • 9
  • 11