【问题标题】:Work with GitHub Multiple Accounts使用 GitHub 多个帐户
【发布时间】:2017-07-02 14:03:08
【问题描述】:

我已经按照tutorial为多个github账户设置了SSH密钥。

但我不断收到错误

ssh: Could not resolve hostname github-psdtowordpresspro.com: Name or service not known
fatal: Could not read from remote repository.

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

后续步骤:

  1. 已创建 SSH 密钥:id_rsa_psdtowordpresspro.pub 文件位于 .ssh 目录中。 (请注意,这个文件夹之前不存在。所以我必须创建一个)

使用语法ssh-keygen -t rsa -C "your-email-address"

  1. 将 SSH 密钥添加到我的第二个 github 帐户

  2. 添加密钥

    ssh-add ~/.ssh/id_rsa_COMPANY

  3. 使用命令创建了.config 文件

    触摸 ~/.ssh/config vim 配置

这是我在.config 文件中编写的代码

Host github-regipheirim
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_psdtowordpresspro

这正是教程中提到的方式。

然后我将远程原点添加为

git remote add origin git@github-psdtowordpresspro.com:regipheirim/regi.git

但是当我尝试推送时,它显示“无法解析主机名”的错误。

这里是截图

Git remote connection setting of second account

项目的.config 文件

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
    symlinks = false
    ignorecase = true
[remote "origin"]
    url = git@github-regipheirim:regipheirim/regi.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[user]
    name = regipheirim
    email = p.regidev@gmail.com

也尝试过(基于 TJL 在评论部分的建议)

git remote set-url origin git@github.com:regipheirim/regi.git

但是当我试图用

git push origin master

我再次收到错误:

Permission denied (publickey).
fatal: Could not read from remote repository.

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

所以任何人都可以帮助我解决我在这里做错的事情。我只是花了整个星期天才弄清楚。

【问题讨论】:

  • GIT_SSH_COMMAND="ssh -vvv"

标签: git github ssh


【解决方案1】:

您没有链接到您在配置文件中创建的别名

尝试:

git remote add origin git@github-regipheirim:regipheirim/regi.git

【讨论】:

  • 非常感谢@TLJ。我整天都在犯一个非常愚蠢的错误,而不是 regipheirim 我使用的是我的公司名称......太愚蠢了。现在它确实推动了回购。但奇怪的是它确实从我的第一个主帐户推送。我可以知道为什么吗?我的意思是它应该从第二个帐户中提取,对吗?仅供参考:我使用git push origin master 作为推送命令
  • 我会使用git remote -v 列出已设置的内容。也许origin 设置为@github.com 而不是regipheirim 别名?告诉我
  • origin git@github-regipheirim:regipheirim/regi.git (fetch) origin git@github-regipheirim:regipheirim/regi.git (push) 这是 gir remote -v 返回的内容
  • 尝试删除 repo。并根据您的建议重复整个过程。但它仍然从主账户推送。奇怪
  • 使用git config user.email检查当前电子邮件帐户。我怀疑这是您的主要帐户。如果是这样,请将其设置为您的第二个帐户。使用git config user.email = {youremail}
【解决方案2】:

针对同一服务的多个帐户的解决方案。在这种情况下为 Bitbucket。

1) 打开~/.gitconfig文件

2) 为每个帐户添加部分

例如,对于用户 JohnDoe(在 ~/.ssh/id_rsa_john_1 中使用 rsa 密钥),您应该添加

Host bitbucket.org:JohnDoe
 User JohnDoe
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_rsa_john_1
 IdentitiesOnly yes

此用户名也应该存在于您尝试通过 SSH 克隆的 URL 中,例如:

git clone git@bitbucket.org:JohnDoe/my_project.git

当您在同一服务中拥有不同的帐户时,您也可以添加它:

Host bitbucket.org:RichardRoe
 User RichardRoe
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_rsa_richard_1
 IdentitiesOnly yes

因此,在您的文件中,您将有两个关于 bitbucket.org 的部分。

感谢您可以使用不同的帐户(使用不同的 RSA 密钥)。

3) 运行eval $(ssh-agent)

4) 为所有帐户使用ssh-add

所以对于这个例子,它将是:

ssh-add -K ~/.ssh/id_rsa_john_1

ssh-add -K ~/.ssh/id_rsa_richard_1

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2023-03-07
    • 2011-03-14
    相关资源
    最近更新 更多