1

I am trying to build a go application that makes use of git repositories that are private. I have an ssh key for my github account and have the following in my .gitconfig file:

[url "https://username:token@github.com"]
    insteadOf = https://github.com
[url "ssh://git@github.com/"]
    insteadOf = https://github.com/

when I execute go test or go build I am asked for the passphrase. I then get a response:

go: found github.com/x/businesses in github.com/x/businesses v0.0.0-yy
go: cmd/main/cmd imports
  github.com/x/businesses: github.com/x/businesses@v0.0.0-yy/go.mod: 
  verifying module: github.com/x/businesses@v0.0.0-yy/go.mod: 
  reading https://sum.golang.org/lookup/github.com/xx/businesses@v0.0.0-yy: 410 Gone
server response:
  not found: github.com/x/businesses@v0.0.0-yy: 
  invalid version: git fetch -f origin refs/heads/*:refs/heads/* refs/tags/*:refs/tags/* 
  in tmp/gopath/pkg/mod/cache/vcs/zz: 
    exit status 128:
    fatal: could not read Username for 'https://github.com': terminal prompts disabled

I tried removing the top insteadOf in the .gitconfig for no reason other then to try something.

Nefarious
  • 458
  • 6
  • 21
  • Have a look at [`GOPRIVATE`](https://golang.org/ref/mod#environment-variables). – Benjamin W. Jan 06 '21 at 20:03
  • have you tried setting env GIT_TERMINAL_PROMPT=1 ? – Althaf M Jan 06 '21 at 21:08
  • like mentioned here https://stackoverflow.com/questions/32232655/go-get-results-in-terminal-prompts-disabled-error-for-github-private-repo – Althaf M Jan 06 '21 at 21:09
  • another option is to add a .netrc file in rather than using insteadOf, here https://ec.haxx.se/usingcurl/usingcurl-netrc , file format need to give GitHub.com domain name and username and password, it works for me – Althaf M Jan 06 '21 at 21:10

1 Answers1

3

Your git config should look something like

[url "git@github.com:"] insteadOf = https://github.com/

or similar to what you have.

Then you can go get your pacakge by telling Go that your using a private repo like this:

GOPRIVATE="github.com/your_username_or_org" go get github.com/name_or_org/repo_name

Also, I usually dont put a passprahse on my SSH keys since is kinda annoying to type it in each time but of course is more secure by adding it like someone pointed out in the comments.

Marlon Monroy
  • 144
  • 1
  • 4
  • 1
    A key with passphase is more secure and it is good practice to use it in case someone access your key. It encrypts your key and you have more time to change key until someone find password(in the worst scenario) You can use ssh-agent when ssh passphase is present. – Daniel Hornik Jan 06 '21 at 20:07