我正在寻找获得$ go与私人存储库工作的方法,经过许多谷歌尝试。

第一次尝试:

$ go get -v gitlab.com/secmask/awserver-go
Fetching https://gitlab.com/secmask/awserver-go?go-get=1
https fetch failed.
Fetching http://gitlab.com/secmask/awserver-go?go-get=1
Parsing meta tags from http://gitlab.com/secmask/awserver-go?go-get=1 (status code 200)
import "gitlab.com/secmask/awserver-go": parse http://gitlab.com/secmask/awserver-go?go-get=1: no go-import meta tags
package gitlab.com/secmask/awserver-go: unrecognized import path "gitlab.com/secmask/awserver-go

是的,它没有看到元标签,因为我不知道如何提供登录信息。

第二次尝试:

按照https://gist.github.com/shurcooL/6927554。将config添加到.gitconfig。

[url "ssh://git@gitlab.com/"]
    insteadOf = https://gitlab.com/
$ go get -v gitlab.com/secmask/awserver-go --> not work
$ go get -v gitlab.com/secmask/awserver-go.git --> work but I got src/gitlab.com/secmask/awserer-go.git

是的,它的工作,但与。git扩展与我的项目名称,我可以重命名为原来的,但每次做$ go get不太好,有没有其他方法?


当前回答

以上所有的方法对我都不起作用。克隆回购工作正确,但我仍然得到一个无法识别的导入错误。

因为它代表Go v1.13,我在文档中发现我们应该像这样使用GOPRIVATE env变量:

$ GOPRIVATE=github.com/ORGANISATION_OR_USER_NAME go get -u github.com/ORGANISATION_OR_USER_NAME/REPO_NAME

其他回答

我有一个问题,从我们公司的gitlab使用私人存储库。 我花了几分钟试图找到解决办法。我找到了这个:

你需要获得一个私人令牌: https://gitlab.mycompany.com/profile/account 配置你的git添加额外的头与你的私有令牌: $ git配置——global http。"PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN" 配置你的git将请求从http转换为ssh: $ git配置——全局url."git@gitlab.mycompany.com:". insteadof "https://gitlab.mycompany.com/" 最后你可以正常使用你的go get: $ go get gitlab.com/company/private_repo

正确的方法是手动将存储库放在正确的位置。一旦有了存储库,您可以使用go get -u来更新包,然后使用install来安装它。一个名为

github.com/secmask/awserver-go

进入

$GOPATH/src/github.com/secmask/awserver-go

您键入的命令是:

cd $GOPATH/src/github.com/secmask
git clone git@github.com:secmask/awserver-go.git

我已经创建了一个特定于用户的ssh-config,因此我的用户可以使用正确的凭证和密钥自动登录。

首先,我需要生成一个密钥对

ssh-keygen -t rsa -b 4096 -C "my@email.here"

并保存到例如~/.ssh/id_my_domain。请注意,这也是我连接到我的Github帐户的对口令(私有和公共),所以我的存储在~/.ssh/id_github_com。

然后我创建(或修改)了一个名为~/的文件。Ssh /config有一个条目:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_github_com

在另一个服务器上,“ssh-url”是admin@domain.com:username/private-repo.git,这个服务器的条目应该是:

Host domain.com
    HostName domain.com
    User admin
    IdentityFile ~/.ssh/id_domain_com

澄清一下,您需要确保正确设置了User、Host和HostName。

现在我可以浏览到go路径,然后去get <package>,例如去get main文件main/main。Go包含包(来自上一个例子)domain.com:username/private-repo.git。

我偶然发现了.netrc,并发现它与此相关。

创建一个包含以下内容的文件~/.netrc:

machine github.com
    login <github username>
    password <github password or Personal access tokens >

完成了!

此外,对于最新的GO版本,您可能需要将其添加到环境变量GOPRIVATE=github.com (我已经把它添加到我的。zshrc)

netrc还使我的开发环境设置更好,因为我个人的github访问HTTPS现在已配置为跨机器使用(就像我的SSH配置)。

生成GitHub个人访问令牌:https://github.com/settings/tokens

请参阅这个答案,了解它在Windows上与Git的具体使用

参考:netrc手册页


如果您希望坚持使用SSH身份验证,那么可以屏蔽请求以强制使用SSH

git配置——全局url."git@github.com:". insteadof "https://github.com/"


更多设置git访问的方法:https://gist.github.com/technoweenie/1072829#gistcomment-2979908

我第一次尝试

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

但在我的本地车里,它不管用。

我试着

SSH -t git@github.com

它显示我的SSH是好的。

最后,我解决了这个问题,告诉go get考虑所有的私有和使用ssh而不是HTTPS。

添加export GOPRIVATE=*