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

时间:2016-12-23 02:17:43

标签: git push gitlab

我在~/.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 =摩西toh,用户名2 = moschel(我在2年前创建此帐号)

名称2 =摩西,用户名2 = moses639(我昨天创建了此帐户)

然后我尝试推送到gitlab

  1. git add。
  2. git commit -m" test push multiple account"
  3. git remote add moschel https://gitlab.com/xxxxx/myproject
  4. git push --set-upstream moschel development
  5. 存在这样的错误:

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

    然后我再次尝试使用其他帐户推送到gitlab

    1. git add。
    2. git commit -m" test push multiple account"
    3. git remote add moses639 https://gitlab.com/xxxxx/myproject
    4. git push --set-upstream moses639 development
    5. 存在这样的错误:

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

1 个答案:

答案 0 :(得分:2)

好吧,首先你的测试是错误的。关键是不要给身份文件提供命令,而是通过主机名,所以试试ssh git@one.gitlab.comssh git@two.gitlab.com,你应该得到不同的名字。您的SSH配置很快就会看起来很好。

另一点是,你说“尝试推送”和“尝试推送另一个帐户”,但你不这样做。如果您认为git remote add moses639 https://gitlab.com/xxxxx/myproject将以用户moses639推送,这是完全错误的,因为这只是您配置的遥控器的本地名称,您可以将其命名为foo,它仍然没有区别

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