如何使用多个帐户推送到gitlab?

How to push to gitlab with multiple account?

我在 ~/.ssh/config 中这样设置:

#Account one
Host one.gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa

#Account two
Host two.gitlab.com
    HostName gitlab.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_mastersystem

然后我尝试这样测试它:

成功

注:

姓名 1 = moses toh,用户名 2 = moschel(我大约 2 年前创建了这个帐户)

name 2 = moses, username 2 = moses639 (我昨天注册的)

然后我尝试推送到 gitlab

  1. git 添加 .
  2. git commit -m "test push multiple account"
  3. git 远程添加 moschel https://gitlab.com/xxxxx/myproject
  4. git push --set-upstream moschel development

存在这样的错误:

remote: Access denied
fatal: unable to access 'https://gitlab.com/xxxxx/myproject.git/': The requested URL returned error: 403

然后我再次尝试使用另一个帐户推送到 gitlab

  1. git 添加 .
  2. git commit -m "test push multiple account"
  3. git远程添加moses639https://gitlab.com/xxxxx/myproject
  4. git push --set-upstream moses639 development

存在这样的错误:

remote: Access denied
fatal: unable to access 'https://gitlab.com/xxxxx/myproject.git/': The requested URL returned error: 403

嗯,首先你的测试是错误的。重点不是通过命令给出身份文件,而是通过主机名,所以尝试 ssh git@one.gitlab.comssh git@two.gitlab.com 你应该会收到不同的名字。快速浏览一下您的 SSH 配置看起来不错。

另一点是,你说"try push"和"try push with another account"但你不做。如果您认为 git remote add moses639 https://gitlab.com/xxxxx/myproject 将作为用户 moses639 推送,这是完全错误的,因为那只是您配置的遥控器的本地名称,您可以将其命名为 foo 并且它仍然没有区别.

此外,您使用 https 网址。如果您想通过 SSH 配置文件 select 身份,这没有帮助。您必须通过 SSH 协议克隆(或实际推送),然后不使用 gitlab.com 作为主机名,而是使用 one.gitlab.comtwo.gitlab.com,具体取决于您要使用的身份。实际上你可以使用 gitlab.comtwo.gitlab.com,因为 one.gitlab.com 无论如何都标识默认密钥。