Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to `go get` private repos using SSH key auth instead of password auth.
$ ssh -A vm
$ git config --global url."git@github.com:".insteadOf "https://github.com/"
$ cat ~/.gitconfig
[url "git@github.com:"]
	insteadOf = https://github.com/
$ go get github.com/private/repo && echo Success!
Success!

Sources:

@mkrull

This comment has been minimized.

Show comment Hide comment
@mkrull

mkrull Jan 21, 2015

This will cause problems with go get -u github.com/private/repo later on though

mkrull commented Jan 21, 2015

This will cause problems with go get -u github.com/private/repo later on though

@anphung

This comment has been minimized.

Show comment Hide comment
@anphung

anphung Jan 22, 2015

$ go get -u github.com/private/repo workaround:

$ cd $GOPATH/src/github.com/private/repo && git pull

anphung commented Jan 22, 2015

$ go get -u github.com/private/repo workaround:

$ cd $GOPATH/src/github.com/private/repo && git pull
@shurcooL

This comment has been minimized.

Show comment Hide comment
@shurcooL

shurcooL Jan 31, 2015

This will cause problems with go get -u github.com/private/repo later on though

@mkrull, you are right. That's a change introduced with Go 1.4 (which is after I came up with this solution).

@anphung, an alternative workaround would be to use go get -u -f github.com/private/repo:

The -f flag, valid only when -u is set, forces get -u not to verify that each package has been checked out from the source control repository implied by its import path. This can be useful if the source is a local fork of the original.

Owner

shurcooL commented Jan 31, 2015

This will cause problems with go get -u github.com/private/repo later on though

@mkrull, you are right. That's a change introduced with Go 1.4 (which is after I came up with this solution).

@anphung, an alternative workaround would be to use go get -u -f github.com/private/repo:

The -f flag, valid only when -u is set, forces get -u not to verify that each package has been checked out from the source control repository implied by its import path. This can be useful if the source is a local fork of the original.

@drichardson

This comment has been minimized.

Show comment Hide comment
@drichardson

drichardson Aug 1, 2015

I wrote a script called gogetprivate, which first tries go get, and then falls back to git clone. My version on works with bitbucket right now (since that's where my private repos are) but I imagine it could be made to work with github as well.

https://github.com/drichardson/gogetprivate

I wrote a script called gogetprivate, which first tries go get, and then falls back to git clone. My version on works with bitbucket right now (since that's where my private repos are) but I imagine it could be made to work with github as well.

https://github.com/drichardson/gogetprivate

@bobziuchkovski

This comment has been minimized.

Show comment Hide comment
@bobziuchkovski

bobziuchkovski Feb 7, 2016

An alternative to using git@github.com is to generate a personal access token on your GitHub account, grant it repo access, and then use the following instead:

git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/"

That should still work with go get -u, and also works with Docker builds (my particular use case, and how I came across your gist).

An alternative to using git@github.com is to generate a personal access token on your GitHub account, grant it repo access, and then use the following instead:

git config --global url."https://${GITHUB_TOKEN}:x-oauth-basic@github.com/".insteadOf "https://github.com/"

That should still work with go get -u, and also works with Docker builds (my particular use case, and how I came across your gist).

@pedrocarrico

This comment has been minimized.

Show comment Hide comment
@pedrocarrico

pedrocarrico Feb 11, 2016

Thanks @bobziuchkovski that seems like a good alternative 👍

Thanks @bobziuchkovski that seems like a good alternative 👍

@michael-px

This comment has been minimized.

Show comment Hide comment
@michael-px

michael-px Jun 1, 2016

This doesn't currently work on go 1.5.1. none of the config --global options work.

This doesn't currently work on go 1.5.1. none of the config --global options work.

@abbviemr

This comment has been minimized.

Show comment Hide comment
@abbviemr

abbviemr Sep 4, 2016

Worked just fine for me on go 1.7. Thank you @bobziuchkovski!

abbviemr commented Sep 4, 2016

Worked just fine for me on go 1.7. Thank you @bobziuchkovski!

@AlexanderPavlenko

This comment has been minimized.

Show comment Hide comment
@AlexanderPavlenko

AlexanderPavlenko Feb 2, 2017

Credentials may be stored in the ~/.git-credentials file.

git config --global credential.helper store
echo "https://${GITHUB_TOKEN}:x-oauth-basic@github.com" >> ~/.git-credentials

git:// protocol is less secure

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

Credentials may be stored in the ~/.git-credentials file.

git config --global credential.helper store
echo "https://${GITHUB_TOKEN}:x-oauth-basic@github.com" >> ~/.git-credentials

git:// protocol is less secure

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

This comment has been minimized.

Show comment Hide comment
@akillmer

akillmer Feb 3, 2017

I got a Raspberry Pi that I wanted to go get some private repos with; after trying a few different solutions it was the one suggested by @bobziuchkovski that did the trick -- thank you! (FWIW, using Git 2.11 and Golang 1.7.5 linux/arm)

akillmer commented Feb 3, 2017

I got a Raspberry Pi that I wanted to go get some private repos with; after trying a few different solutions it was the one suggested by @bobziuchkovski that did the trick -- thank you! (FWIW, using Git 2.11 and Golang 1.7.5 linux/arm)

@nikhilmahesh

This comment has been minimized.

Show comment Hide comment
@nikhilmahesh

nikhilmahesh Feb 15, 2017

Thanks @bobziuchkovski , Your solution helped !

Thanks @bobziuchkovski , Your solution helped !

@dcyou

This comment has been minimized.

Show comment Hide comment
@dcyou

dcyou Mar 22, 2017

Thanks too @bobziuchkovski

dcyou commented Mar 22, 2017

Thanks too @bobziuchkovski

@likewei92

This comment has been minimized.

Show comment Hide comment
@likewei92

likewei92 Nov 2, 2017

I think @AlexanderPavlenko's solution is much better. Thanks!

I think @AlexanderPavlenko's solution is much better. Thanks!

@AndrewSav

This comment has been minimized.

Show comment Hide comment
@AndrewSav

AndrewSav Jan 1, 2018

@AlexanderPavlenko

git:// protocol is less secure

Sorry, what? Where did you read that?

@AlexanderPavlenko

git:// protocol is less secure

Sorry, what? Where did you read that?

@elyscape

This comment has been minimized.

Show comment Hide comment
@elyscape

elyscape Mar 1, 2018

@AndrewSav From Pro Git:

[The Git protocol] listens on a dedicated port (9418) that provides a service similar to the SSH protocol, but with absolutely no authentication.
[...]
The downside of the Git protocol is the lack of authentication. It’s generally undesirable for the Git protocol to be the only access to your project. Generally, you’ll pair it with SSH or HTTPS access for the few developers who have push (write) access and have everyone else use git:// for read-only access.

This is why git+ssh:// and git+https:// exist.

elyscape commented Mar 1, 2018

@AndrewSav From Pro Git:

[The Git protocol] listens on a dedicated port (9418) that provides a service similar to the SSH protocol, but with absolutely no authentication.
[...]
The downside of the Git protocol is the lack of authentication. It’s generally undesirable for the Git protocol to be the only access to your project. Generally, you’ll pair it with SSH or HTTPS access for the few developers who have push (write) access and have everyone else use git:// for read-only access.

This is why git+ssh:// and git+https:// exist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment