【问题标题】:How to set up two SSH keys for two GitLab accounts and push/pull by using TortoiseGit?如何使用 TortoiseGit 为两个 GitLab 帐户设置两个 SSH 密钥并推/拉?
【发布时间】:2015-07-30 23:25:44
【问题描述】:

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

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

密钥已被占用
指纹已被采集

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

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

提前致谢!!

更新

下面是我的配置文件。如下:

#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 帐户,

git@gitlab.com:{account_1}/repo1.git
git@gitlab.com:{account_2}/repo1.git

我仍然无法访问account_2

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

【问题讨论】:

标签: git ssh key gitlab tortoisegit


【解决方案1】:

只需在 %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

假设您的 ssh 密钥集是:

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

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

gitlabuser1:yourRepo1
gitlabuser2:yourRepo2

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

您有更详细的程序in this blog post

【讨论】:

  • 我倾向于相信只写IdentityFile id_rsa2 应该从~/.ssh/ 中选择id_rsa2 文件,从而自动做正确的事情。我会试试看。
  • hte gitlabuser1 的正确格式应该是什么。是这样的吗:git@gitlab.com:my_username
  • @GordonQu gitlabuser1 可以替换 git@gilab.com。请注意,对于 ssh url,username 是 url never 的一部分,因为公共 ssh 密钥的工作是在 GitLab 服务器端对您进行身份验证。
  • 那么{hostname}呢,是不是也一样,应该也是git@gitlab.com吧?
  • @GordonQu 如果您使用Host {account2},那么要使用的网址将是{account_2}:{account_2}/repo1.git。忘记git@gitlab.comHost 是一个。如果您已将Host 设置为xxx2,则网址将为xxx2:{account2}/repo1.git
猜你喜欢
  • 2015-06-28
  • 2022-10-23
  • 2020-03-01
  • 2022-09-23
  • 2016-10-20
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多