How to "go get" from private repository

venom90

Tirumal Rao

Posted on June 23, 2022

How to "go get" from private repository

Installing a go package is breeze with the introduction of go mod. However, when it comes to getting (installing) packages from private repository then it's not that straight forward.

In this post, I'll discuss how to go get from a private repository. I'll show how to get packages from Github.com, but method is almost same for other repository hosting services.

OPERATING SYSTEM: Mac or Linux

STEP 1: Create SSH Key

In this step I'll generate SSH key, in my case I've multiple Github Accounts. One for is personal and another one for my organization I'm currently working with.

Please make sure that you give it a unique file name when asked.

ssh-keygen
Enter fullscreen mode Exit fullscreen mode

Enter a name and select the default location ~/.ssh

Enter file in which to save the key (/Users/<username>/.ssh/id_rsa):
Enter fullscreen mode Exit fullscreen mode

STEP 2: SSH Config file

This step is optional. I'm creating a config file to use multiple SSH keys for multiple GitHub/Bitbucket/Gitlab accounts.

CREATE(if not exist) a file config in ~/.ssh and enter Host configuration for particular SSH keys and Github account.

Here is a sample:

# Bitbucket Account
Host acme
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/acme
    IdentitiesOnly yes

# Bitbucket Account
Host simpson
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/simpson
    IdentitiesOnly yes

# GitHub Account
Host donald
    HostName github.com
    User git
    IdentityFile ~/.ssh/donald
    IdentitiesOnly yes
Enter fullscreen mode Exit fullscreen mode

STEP 3: Create a Global Git Config

On you terminal enter following command and execute

git config --global url.git@acme:<GITHUB_USER_NAME>.insteadOf https://github.com/<GITHUB_USER_NAME>
Enter fullscreen mode Exit fullscreen mode

Here, acme is the Host configuration from ~/.ssh/config file.

Replace, with your Github username.

If done correctly, the file at location ~/.gitconfig will be modified. The above configuration will added to the EOF.

STEP 4: Now go get 😊

go get github.com/<GITHUB_USER_NAME>/private-repo
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
venom90
Tirumal Rao

Posted on June 23, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related