本文介绍了如何为两个 GitLab 帐户设置两个 SSH 密钥并使用 TortoiseGit 进行推/拉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 帐户并且我正尝试为其使用相同的 SSH 密钥,但是我无法将密钥添加到这个新帐户.
我尝试添加密钥时的错误如下:

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 帐户?如果不可能,我应该如何同时使用两个键.

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.

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

By the way, I'm using windows system.

提前致谢!!

更新

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

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

而且我有两个 Gitlab 帐户,

And I'm having two Gitlab account,

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

仍然无法访问 account_2.

以前,在我拥有第二个 GitLab 帐户之前,我只需将 ssh 密钥上传到 account1 而无需设置 这个.但是现在按照这个,最后我仍然可以推送到 [email protected]:{account_2}/repo1.git.我使用 TortoiseGit 来推/拉.

Previously, before I'm having this 2nd GitLab account, I simply upload the ssh key to the account1 without 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 TortoiseGit to push/pull.

推荐答案

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

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

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

假设您的 ssh 密钥集是:

That supposes your set of ssh keys are:

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

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

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

gitlabuser1:yourRepo1
gitlabuser2:yourRepo2

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

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

您在此博客中有更详细的过程发布.

You have a more detailed procedure in this blog post.

这篇关于如何为两个 GitLab 帐户设置两个 SSH 密钥并使用 TortoiseGit 进行推/拉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 04:13