在 gitlab 中使用 2 个帐户的 ssh 不起作用

ssh with 2 accounts in gitlab doesn't work

我正在尝试在我的电脑中添加 2 个 ssh 密钥以使用 2 个 gitlab 帐户,我创建了 2 对密钥并制作了这个“配置”文件:

# Compte Perso
Host gitlab.com-perso
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile C:\Users\<path>\.ssh\id_rsa_perso

# Compte Pro
Host gitlab.com
   HostName gitlab.com
   PreferredAuthentications publickey
   IdentityFile C:\Users\<path>\.ssh\id_rsa

当我键入 ssh -T git@gitlab.comssh -T git@gitlab.com-perso 时,我得到了输出 Wlecome to Gitlab, <username>!,但当我尝试像这样克隆时 git clone git@gitlab.com-perso:<username>/<reponame>.git

它说

Unable to open connection:
Host does not existfatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我知道 repo 可以被克隆,因为我已经用 ssh 将它克隆到另一台电脑上,但我不明白为什么这不起作用,请帮助我。 我已经按照 this 制作了我的配置文件。 (这是法语)

快速 google search 搜索您的错误消息,发现此问题可能与“Little Snitch”软件有关 - 如果您使用它,请禁用它并重试。

此外,在 Windows 台机器上,您可能需要修改 windows 系统环境变量

GIT_SSH=C:\Program Files\Git\usr\bin\ssh.exe

如您所见this Whosebug thread

如果 none 有效,还有其他一些想法。至少尝试其中几个并收集一些更详细的输出是个好主意,这样我们可以在需要时更好地帮助您。

首先,您可以在每个条目中添加 User git。这样,您可以在没有 git@

的情况下使用 gitlab.com-perso:<usernassh -T git@gitlab.com-persome>/<reponame>.git

其次,确保 ssh -T git@gitlab.com-perso return 正确的帐户,因为如果从您的问题看来 return 是相同的用户名。

Host does not exist

仔细检查您的主机条目:gitlab。com-perso 应该使用正确的“-”:您可能使用了“—”(long dash) instead of the expected double hyphen (or hyphen-minus)。

感谢@Lukasz Korbasiewicz 和@bdecaf 我在配置文件中添加了 IdentitiesOnly yes ~/.ssh/config 并在环境变量中添加了 GIT_SSH=C:\Program Files\Git\usr\bin\ssh.exe 并且它起作用了。