git “获取”私有存储库的正确方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27500861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the proper way to "go get" a private repository?
提问by secmask
I'm searching for the way to get $ go get
work with private repository, after many google try.
$ go get
经过多次谷歌尝试后,我正在寻找使用私有存储库的方法。
The first try:
第一次尝试:
$ 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
Yep, it did not see the meta tags because I could not know how to provide login information.
是的,它没有看到元标记,因为我不知道如何提供登录信息。
The second try:
第二次尝试:
Follow https://gist.github.com/shurcooL/6927554. Add config to .gitconfig.
按照https://gist.github.com/shurcooL/6927554 进行操作。将配置添加到 .gitconfig。
[url "ssh://[email protected]/"]
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
Yes it work but with .git
extension with my project name, I can rename it to original but do it everytime $ go get
is not so good, is there an otherway?
是的,它可以工作,但使用.git
我的项目名称进行扩展,我可以将其重命名为原始名称,但每次$ go get
都这样做不太好,还有其他方法吗?
回答by JulienD
You have one thing to configure. The example is based on GitHub but this shouldn't change the process:
您需要配置一件事。该示例基于 GitHub,但这不应改变流程:
$ git config --global [email protected]:.insteadOf https://github.com/
$ cat ~/.gitconfig
[url "[email protected]:"]
insteadOf = https://github.com/
$ go get github.com/private/repo
回答by fuz
The proper way is to manually put the repository in the right place. Once the repository is there, you can use go get -u
to update the package and go install
to install it. A package named
正确的方法是手动将存储库放在正确的位置。一旦存储库在那里,您就可以使用它go get -u
来更新包并go install
安装它。一个名为的包
github.com/secmask/awserver-go
goes into
进入
$GOPATH/src/github.com/secmask/awserver-go
The commands you type are:
您键入的命令是:
cd $GOPATH/src/github.com/secmask
git clone [email protected]:secmask/awserver-go.git
回答by Rodrigo Oliveira
I had a problem with go get
using private repository on gitlabfrom our company.
I lost a few minutes trying to find a solution. And I did find this one:
我在我们公司的gitlab上go get
使用私有存储库时遇到问题。我失去了几分钟试图找到解决方案。我确实找到了这个:
You need to get a private token at:
https://gitlab.mycompany.com/profile/accountConfigure you git to add extra header with your private token:
$ git config --global http.extraheader "PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN
Configure your git to convert requests from httpto ssh:
$ git config --global url."[email protected]:".insteadOf "https://gitlab.mycompany.com/"
Finally you can use your
go get
normally:$ go get gitlab.com/company/private_repo
您需要在以下位置获取私有令牌:https:
//gitlab.mycompany.com/profile/account配置您的 git 以使用您的私有令牌添加额外的标头:
$ git config --global http.extraheader "PRIVATE-TOKEN: YOUR_PRIVATE_TOKEN
配置您的 git 以将请求从http转换为ssh:
$ git config --global url."[email protected]:".insteadOf "https://gitlab.mycompany.com/"
最后,您可以
go get
正常使用:$ go get gitlab.com/company/private_repo
回答by VonC
That looks like the GitLab issue 5769.
这看起来像GitLab 问题 5769。
In GitLab, since the repositories always end in
.git
, I must specify.git
at the end of the repository name to make it work, for example:import "example.org/myuser/mygorepo.git"
And:
$ go get example.org/myuser/mygorepo.git
Looks like GitHub solves this by appending
".git"
.
在 GitLab 中,由于存储库总是以 结尾
.git
,我必须.git
在存储库名称的末尾指定才能使其工作,例如:import "example.org/myuser/mygorepo.git"
和:
$ go get example.org/myuser/mygorepo.git
看起来 GitHub 通过附加
".git"
.
It is supposed to be resolved in “Added support for Go's repository retrieval. #5958”, provided the right meta tags are in place.
Although there is still an issue for Go itself: “cmd/go
: go get cannot discover meta tag in HTML5 documents”.
它应该在“添加对 Go 的存储库检索的支持中解决。#5958”,前提是有正确的元标记。
尽管 Go 本身仍然存在一个问题:“ cmd/go
: go get 无法发现 HTML5 文档中的元标记”。
回答by Robert K. Bell
If you've already got git using SSH, this answerby Ammar Bandukwalais a simple workaround:
如果您已经使用SSH已经有了GIT中,这个答案由阿马尔Bandukwala是一个简单的解决方法:
$ go get
uses git
internally. The following one liners will make git
and consequently $ go get
clone your package via SSH.
$ go get
git
内部使用。下面的一个衬垫将通过 SSH制作git
并因此$ go get
克隆您的包。
Github:
GitHub:
$ git config --global url."[email protected]:".insteadOf "https://github.com/"
BitBucket:
比特桶:
$ git config --global url."[email protected]:".insteadOf "https://bitbucket.org/"
回答by jolancornevin
All of the above did not work for me. Cloning the repo was working correctly but I was still getting an unrecognized import
error.
以上所有对我都不起作用。克隆 repo 工作正常,但我仍然遇到unrecognized import
错误。
As it stands for Go v1.13, I found in the doc that we should use the GOPRIVATEvariable like so:
由于它代表 Go v1.13,我在文档中发现我们应该像这样使用GOPRIVATE变量:
GOPRIVATE=github.com/ORGANISATION_OR_USER_NAME go get -u github.com/ORGANISATION_OR_USER_NAME/REPO_NAME
回答by Miguel Mota
Generate a github oauth token hereand export your github token as an environment variable:
在此处生成 github oauth 令牌并将您的 github 令牌导出为环境变量:
export GITHUB_TOKEN=123
Set git config to use the basic auth url:
设置 git config 以使用基本身份验证 URL:
git config --global url."https://$GITHUB_TOKEN:[email protected]/".insteadOf "https://github.com/"
Now you can go get
your private repo.
现在您可以使用go get
您的私人仓库。
回答by Joachim
I have created a user specific ssh-config, so my user automatically logs in with the correct credentials and key.
我创建了一个特定于用户的 ssh-config,因此我的用户会使用正确的凭据和密钥自动登录。
First I needed to generate an key-pair
首先我需要生成一个密钥对
ssh-keygen -t rsa -b 4096 -C "[email protected]"
and saved it to e.g ~/.ssh/id_my_domain
. Note that this is also the keypair (private and public) I've connected to my Github account, so mine is stored in~/.ssh/id_github_com
.
并将其保存到例如~/.ssh/id_my_domain
. 请注意,这也是我连接到 Github 帐户的密钥对(私有和公共),因此我的存储在~/.ssh/id_github_com
.
I have then created (or altered) a file called ~/.ssh/config
with an entry:
然后我创建(或更改)了一个~/.ssh/config
带有条目的文件:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_github_com
On another server, the "ssh-url" is [email protected]:username/private-repo.git
and the entry for this server would have been:
在另一台服务器上,“ssh-url”是[email protected]:username/private-repo.git
,该服务器的条目应该是:
Host domain.com
HostName domain.com
User admin
IdentityFile ~/.ssh/id_domain_com
Just to clarify that you need ensure that the User
, Host
and HostName
is set correctly.
只是为了澄清您需要确保User
,Host
和HostName
设置正确。
Now I can just browse into the go path and then go get <package>
, e.g go get main
where the file main/main.go
includes the package (from last example above) domain.com:username/private-repo.git
.
现在我可以浏览 go 路径,然后go get <package>
,例如go get main
文件main/main.go
包含包的位置(来自上面的最后一个示例)domain.com:username/private-repo.git
。
回答by Tarmo
For me, the solutions offered by others still gave the following error during go get
对我来说,其他人提供的解决方案在执行过程中仍然出现以下错误 go get
[email protected]: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
[email protected]:权限被拒绝(公钥)。致命:无法从远程存储库读取。请确保您拥有正确的访问权限并且存储库存在。
What this solution required
此解决方案需要什么
As stated by others:
git config --global url."[email protected]:".insteadOf "https://github.com/"
Removing the passphrase from my
./ssh/id_rsa
key which was used for authenticating the connection to the repository. This can be done by entering an empty password when prompted as a response to:ssh-keygen -p
正如其他人所说:
git config --global url."[email protected]:".insteadOf "https://github.com/"
从我的
./ssh/id_rsa
密钥中删除密码短语,该密码短语用于验证与存储库的连接。这可以通过在提示时输入空密码来完成:ssh-keygen -p
Why this works
为什么这有效
This is not a pretty workaround as it is always better to have a passphrase on your private key, but it was causing issues somewhere inside OpenSSH.
这不是一个很好的解决方法,因为在您的私钥上设置密码总是更好,但它在 OpenSSH 内部的某个地方引起了问题。
go get
uses internally git, which uses openssh to open the connection. OpenSSH takes the certs necessary for authentication from .ssh/id_rsa
. When executing git commands from the command line an agent can take care of opening the id_rsa file for you so that you do not have to specify the passphrase every time, but when executed in the belly of go get, this did not work somewhy in my case. OpenSSH wants to prompt you then for a password but since it is not possible due to how it was called, it prints to its debug log:
go get
内部使用 git,它使用 openssh 打开连接。OpenSSH 从.ssh/id_rsa
. 从命令行执行 git 命令时,代理可以负责为您打开 id_rsa 文件,这样您就不必每次都指定密码短语,但是在 go get 的腹部执行时,这在我的案件。OpenSSH 想提示您输入密码,但由于它的调用方式而无法实现,因此它会打印到其调试日志中:
read_passphrase: can't open /dev/tty: No such device or address
read_passphrase: 无法打开 /dev/tty: 没有这样的设备或地址
And just fails. If you remove the passphrase from the key file, OpenSSH will get to your key without that prompt and it works
只是失败了。如果您从密钥文件中删除密码短语,OpenSSH 将在没有该提示的情况下获取您的密钥并且可以正常工作
This mightbe caused by Go fetching modules concurrently and opening multiple SSH connections to Github at the same time (as described in this article). This is somewhat supported by the fact that OpenSSH debug log showed the initial connection to the repository succeed, but later tried it again for some reason and this time opted to ask for a passphrase.
这可能是因为 Go 并发获取模块并同时打开多个到 Github 的 SSH 连接(如本文所述)。OpenSSH 调试日志显示与存储库的初始连接成功,但后来出于某种原因再次尝试,这次选择要求密码短语,这一事实在一定程度上支持了这一点。
However the solution of using SSH connection multiplexing as put forward in the mentioned article did not work for me. For the record, the author suggested adding the collowing conf to the ssh config file for the affected host:
但是,上述文章中提出的使用 SSH 连接多路复用的解决方案对我不起作用。作为记录,作者建议将以下 conf 添加到受影响主机的 ssh 配置文件中:
ControlMaster auto
ControlPersist 3600
ControlPath ~/.ssh/%r@%h:%p
But as stated, for me it did not work, maybe I did it wrong
但如前所述,对我来说它不起作用,也许我做错了
回答by Kanak Singhal
I came across .netrc
and found it relevant to this.
我遇到.netrc
并发现它与此相关。
Create a file ~/.netrc
with the following content:
创建一个~/.netrc
包含以下内容的文件:
machine github.com
login <github username>
password <github password or Personal access tokens >
Done!
完毕!
Additionally, for latest GO versions, you might need to add this to the environment variables GOPRIVATE=github.com
(I've added it to my .zshrc
)
此外,对于最新的 GO 版本,您可能需要将其添加到环境变量中GOPRIVATE=github.com
(我已将其添加到我的.zshrc
)
netrc
also makes my development environment setup better as my personal github access for HTTPS is been configured now to be used across the machine (just like my SSH configuration).
netrc
也使我的开发环境设置更好,因为我的个人 GitHub HTTPS 访问现在已配置为可在整个机器上使用(就像我的 SSH 配置一样)。
Generate GitHub personal access tokens: https://github.com/settings/tokens
生成 GitHub 个人访问令牌:https: //github.com/settings/tokens
If you want to stick with the SSH authentication, then mask the request to use ssh forcefully
如果您想坚持使用 SSH 身份验证,则屏蔽请求以强制使用 ssh
git config --global url."[email protected]:".insteadOf "https://github.com/"
git config --global url."[email protected]:".insteadOf "https://github.com/"
More methods for setting up git access: https://gist.github.com/technoweenie/1072829#gistcomment-2979908
更多设置git访问的方法:https: //gist.github.com/technoweenie/1072829#gistcomment-2979908
See the man pageand this answerfor its use with Git on Windows specifically