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:
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.
git clone https://github.com/user/private_repo
and used your github.com username for username and the generated personal access token for password.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.
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 :)
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.
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
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/*
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
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.
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):
git config --global --add url."git@github.com:".insteadOf "https://github.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.
Edit: added the --add
flag as suggested by slatunje.