golang: How to `go get` private repos

Recently, I’ve been working on some golang projects at DataSift that depend on some private modules. This means that when I run go get to fetch the module, Github asks me for my username and password. Unfortunately, as I use 2FA I need to generate a new, temporary password for applications that want my password. As go get doesn’t cache credentials, I’d have to do this every time I needed a private repo. Instead, I wanted to force go get to use my SSH key.

Fortunately, as go get uses git, you can change your .gitconfig to force it to use SSH.

$ git config --global url."[email protected]:".insteadOf "https://github.com/"
$ cat ~/.gitconfig
[url "[email protected]:"]
    insteadOf = https://github.com/

Here, we say use [email protected] any time you’d use https://github.com. This works for everything, not just go get. It just has the nice side effect of using your SSH key any time you run go get too.

Michael is a polyglot software engineer, committed to reducing complexity in systems and making them more predictable. Working with a variety of languages and tools, he shares his technical expertise to audiences all around the world at user groups and conferences. You can follow @mheap on Twitter

Thoughts on this post

Wari Wahab 2014-11-18

Unfortunately, this does not work with bitbucket repos 🙁

satisfucktor 2014-12-19

git config –global url.”[email protected]:”.insteadOf “https://bitbucket.org/”

John Russell 2014-12-30

Made my morning better when I found this. Thanks.

Ollie Parsley 2015-02-10

Thanks! Came in useful just now 🙂

David 2015-03-05

Made our day, awesome. Was driving us nuts.

Taylor Etheredge (@Taylor_E) 2015-03-26

Thank you posting this is very helpful. If you are using godep to manage your dependencies, you will need to add the extension .git to you ImportPath statement. Otherwise it will complain about the import path. Here is an example:
“ImportPath”: “github.company-enterpise.com/user/project.git”

eduncan911 2015-05-26

I have been battling this for months. OMFG! Thank you!

go get使用私有仓库 | 为自己写代码 2017-07-06

[…] https://michaelheap.com/golang-how-to-go-get-private-repos/ […]

Leave a comment?

Leave a Reply