git 如何为两个 GitLab 帐户设置两个 SSH 密钥并使用 TortoiseGit 进行推/拉?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30320458/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 11:01:05  来源:igfitidea点击:

How to set up two SSH keys for two GitLab accounts and push/pull by using TortoiseGit?

gitsshkeygitlabtortoisegit

提问by 2342G456DI8

Currently I'm using GitLab as my remote GIT server.
I have no problem using single Gitlab account with SSH key assigned to it.

目前我使用 GitLab 作为我的远程 GIT 服务器。
我使用分配给它的 SSH 密钥的单个 Gitlab 帐户没有问题。

But now I applied another Gitlab account and I'm trying to use the same SSH key for it, but I cannot add the key to this new account.
The error is as follows when I tried to add the key:

但是现在我申请了另一个 Gitlab 帐户,并且我正在尝试为其使用相同的 SSH 密钥,但是我无法将密钥添加到这个新帐户中。
当我尝试添加密钥时,错误如下:

Key has already been taken
Fingerprint has already been taken

钥匙已取
指纹已取

So how should I use the same key to access the second Gitlab account? if it is not possible, how should I use two keys at the same time.

那么我应该如何使用相同的密钥访问第二个 Gitlab 帐户呢?如果不可能,我应该如何同时使用两个键。

By the way, I'm using windows system.

顺便说一下,我使用的是windows系统。

Thanks in advance!!

提前致谢!!

Updates

更新

Below is my config file. And it is as follows:

下面是我的配置文件。它是这样的:

#my primary account
Host {account1}
    User git
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile C:/Users/{username}/.ssh/id_rsa1

#for NPR_HPTG account
Host {account2}
    User git
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile C:/Users/{username}/.ssh/id_rsa2

And I'm having two Gitlab account,

我有两个 Gitlab 帐户,

[email protected]:{account_1}/repo1.git
[email protected]:{account_2}/repo1.git

Still, I cannot access to the account_2.

尽管如此,我还是无法访问account_2.

Previously, before I'm having this 2nd GitLab account, I simply upload the sshkey to the account1without needing set This. But now by following this, still, in the end I could push to the [email protected]:{account_2}/repo1.git. And I'm using TortoiseGitto push/pull.

以前,在我拥有第二个 GitLab 帐户之前,我只需将ssh密钥上传到account1不需要设置This。但是现在通过遵循此操作,最终我仍然可以推送到[email protected]:{account_2}/repo1.git. 我正在使用TortoiseGit推/拉。

回答by VonC

Simply declare each private ssh keys in a %HOME%/.ssh/configfile:

只需在%HOME%/.ssh/config文件中声明每个私有 ssh 密钥:

Host gitlabuser1
    User git
    Hostname {hostname}
    PreferredAuthentications publickey
    IdentityFile C:/Users/{username}/.ssh/id_rsa1

Host gitlabuser2
    User git
    Hostname {hostname}
    PreferredAuthentications publickey
    IdentityFile C:/Users/{username}/.ssh/id_rsa2

That supposes your set of ssh keys are:

假设您的 ssh 密钥集是:

%HOME%/.ssh/id_rsa1 ; %HOME%/.ssh/id_rsa1.pub
%HOME%/.ssh/id_rsa2 ; %HOME%/.ssh/id_rsa2.pub

You can then use the urls for clone/push/pull:

然后,您可以使用 url 进行克隆/推/拉:

gitlabuser1:yourRepo1
gitlabuser2:yourRepo2

Make sure your CMDsession has %HOME%defined, usually to %USERPROFILE%(which is done for you with git-cmd.bat)

确保您的CMD会话已%HOME%定义,通常为%USERPROFILE%(为您完成git-cmd.bat

You have a more detailed procedure in this blog post.

这篇博文中有更详细的过程。