Using Go with a private Github and forks

At Vena we use a private GitHub Enterprise instance. Recently
the Infrastructure team has been using Go and we want to version our Go code there. There were two problems to overcome:

How to use private git repositories

go get will not fetch Go code from Github using the SSH transport (i.e. go get git@github.com:...). To work around this, you must first clone the repository manually:

$ cd $GOPATH/src/private.github.com/org
$ git clone git@private.github.com:org/repo.git

How to work with GitHub forks

If org/repo.git uses Go sub-packages:

import (
    "private.github.com/org/repo/package"
)

And you create a fork of org/repo.git to your user (at
user/repo.git), you can't clone your fork and work on that clone - the path will be $GOPATH/src/private.github.com/user/repo.git and the above import won't work. Go uses a structured directory hierarchy that restricts where files can be located.

To work with your fork with Go, you have to clone the original repository, and then set up your fork as the origin:

$ cd $GOPATH/src/private.github.com/org
$ git clone git@private.github.com:org/repo.git
$ cd repo/
$ git remote rename origin upstream
$ git remote add origin git@private.github.com:user/repo.git

This way, you can work on your fork, push branches to your fork, and create pull requests to the upstream, without any issues.

Discuss on Hacker News