Skip to content

Instantly share code, notes, and snippets.

@dmitshur
Last active September 17, 2023 07:35
Show Gist options
  • Save dmitshur/6927554 to your computer and use it in GitHub Desktop.
Save dmitshur/6927554 to your computer and use it in GitHub Desktop.
[Legacy GOPATH mode] How to `go get` private repos using SSH key auth instead of password auth.

WARNING: This gist was created in 2013 and targets the legacy GOPATH mode. If you're reading this in 2021 or later, you're likely better served by reading https://tip.golang.org/cmd/go/#hdr-Configuration_for_downloading_non_public_code and https://golang.org/ref/mod#private-modules.

$ ssh -A vm
$ git config --global url."git@github.com:".insteadOf "https://github.com/"
$ cat ~/.gitconfig
[url "git@github.com:"]
	insteadOf = https://github.com/
$ go get github.com/private/repo && echo Success!
Success!

Sources:

@lempzz
Copy link

lempzz commented Jul 27, 2020

The @bobziuchkovski's comment wasn't help me.

But i'm tried to execute next commands:

git config --global url."https://<username>:<access_token>@private-git.com/".insteadOf "https://private-git.com/"

go get private-git.com/owner/repo

@g00nix
Copy link

g00nix commented Jul 27, 2020

As a note to everybody here. When adding the overwrite to .gitconfig like so:

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

you will also need your env var:

export GOPRIVATE="example.com"

This should actually be part of the official docs.

@bhundven
Copy link

bhundven commented Jul 31, 2020

hrm, what about sub-projects on gitlab?

$ go get gitlab.com/SomeOrg/sub-project/project
# cd .; git clone -- https://gitlab.com/SomeOrg/sub-project.git /home/bhundven/go/src/gitlab.com/SomeOrg/sub-project
Cloning into '/home/bhundven/go/src/gitlab.com/SomeOrg/sub-project'...
Warning: Permanently added 'gitlab.com,172.65.251.78' (ECDSA) to the list of known hosts.
remote: 
remote: ========================================================================
remote: 
remote: The project you were looking for could not be found.
remote: 
remote: ========================================================================
remote: 
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
package gitlab.com/SomeOrg/sub-project/project: exit status 128

Tried:

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

@bhundven
Copy link

Maybe not an issue for here, but for here: https://gitlab.com/gitlab-org/gitlab-foss/-/issues/30785

@XVilka
Copy link

XVilka commented Dec 8, 2020

For those who will meet with the same problem: git config --global url."git@github.com:".insteadOf "https://github.com/" works but the local version (git config --local url."git@github.com:".insteadOf "https://github.com/") - doesn't. Likely a git bug.

@no1xsyzy
Copy link

no1xsyzy commented Feb 11, 2021

@XVilka Did you try add local config to $GOPATH/src/github.com/private/repo (by cd into it first)? In fact the local config of that repo is loaded, instead of the repo that you are working on now.

@dmitshur
Copy link
Author

This is a very old gist and may not be applicable by now. I added a warning to the top:

WARNING: This gist was created in 2013 and targets the legacy GOPATH mode. If you're reading this in 2021 or later, you're likely better served by reading https://tip.golang.org/cmd/go/#hdr-Configuration_for_downloading_non_public_code and https://golang.org/ref/mod#private-modules.

@moihn
Copy link

moihn commented May 11, 2021

For those who will meet with the same problem: git config --global url."git@github.com:".insteadOf "https://github.com/" works but the local version (git config --local url."git@github.com:".insteadOf "https://github.com/") - doesn't. Likely a git bug.

I encountered the same issue, even if I tried to setup environment variable GIT_CONFIG to point to my local git config file, it is ignored... Do you have a solution that avoids using global ~/.gitconfig file now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment