Use Go module from private repository

2022-03-10

TL;DR

GOPRIVATE=github.com/<user>/*
GIT_TERMINAL_PROMPT=1
go get github.com/<user>/<repo>

Quick start for GitHub Private Repo

  1. Add private repo url(s) GOPRIVATE environment.
    go env -w GOPRIVATE=github.com/<user>/*
  2. Run go get with enable terminal prompt option
    GIT_TERMINAL_PROMPT=1 go get github.com/<user>/<repo>
  3. If prompt, input:
    Username Your GitHub Username
    Password Your GitHub Access Token

GOPRIVATE

Setting GOPRIVATE to make go get bypass default GOPROXY and checkout from your private repo.
go env -w GOPRIVATE=github.com/<user>/<repo>

  • If you want to append to existing GOPRIVATE, run:
    go env -w GOPRIVATE=$(go env GOPRIVATE),github.com/<user>/repo_a
  • You can also add multiple private repos or using wildcard (_).
    go env -w GOPRIVATE=github.com/<user>/repo_a,github.com/<user>/repo_b
    go env -w GOPRIVATE=github.com/<user>/_
  • If you don’t want to modify the go global environment file, you can run a one-time command
    GOPRIVATE=github.com/<user>/<repo> go get github.com/<user>/<repo>

Authentication

After setting GOPRIVATE, you should choose an authentication method to for git that called by go get.

I am personally recommend this option because it will save your credential in system keychain instead of file.

If you get terminal prompts disabled error after you running go get that means you have not save your credential. To enable terminal prompts, just run:
GIT_TERMINAL_PROMPT=1 go get github.com/<user>/<repo>

Username Your GitHub Username
Password Your GitHub Access Token

Once you input current username and password, you can directly run go get to download go module from private repo without prompt forever.

Remove
macOS: Open Keychain Access.app and search github.com, delete the item with Internet Password type.

Option 2: Modify Git Config

Github (also for GitLab / BitBucket)

  1. Generate GitHub Access Token
  2. Setup global config
    git config --global url."https://<token>@github.com/".insteadOf "https://github.com/"

Remove
git config --global --remove-section url."https://<token>@github.com/"

⚠️ Make sure to add a stroke / at the end of the target url.
url."https://<token>@github.com/".insteadOf "https://github.com/"
url."https://<token>@github.com".insteadOf "https://github.com"

Without the stroke, it may cause security issue. Hacker may setup a fake repo using https://github.com.hacker-site.com (which match https://github.com) to steal your user token.

SSH

  1. Connecting to GitHub with SSH
  2. Setup global config
    git config --global [email protected]:.insteadOf https://github.com/

Remove
git config --global --remove-section [email protected]:

Check / edit git config manually

In case you want to check out current config state or you want to edit it, you can run:
git config --global -e

Option 3: Using .netrc

  1. Generate GitHub Access Token
  2. Create / edit ~/.netrc file and add:
    machine github.com login <token>

Reference

  1. https://www.digitalocean.com/community/tutorials/how-to-use-a-private-go-module-in-your-own-project
  2. https://stackoverflow.com/questions/32232655/go-get-results-in-terminal-prompts-disabled-error-for-github-private-repo

🎉  Thanks for reading and hope you enjoy it. Feel free to check out my other posts and find me on X and GitHub!