git - 对同一 Git 主机上的不同帐户使用不同的 SSH key

标签 git ssh bitbucket

如果我把它作为一个用例来问,这个问题会很简单。我的电脑上有两个 SSH key

  1. personal_id_rsa
  2. company_id_rsa

我在 bitbucket.org 上有两个不同的用户帐户.其中一个是我的个人账户,另一个是我的公司账户。我的 PC 上也有 N 个存储库。其中一些必须与我的个人帐户相关联,一些其他 repo 协议(protocol)必须与公司帐户相关联。没有必须链接到这两个帐户的存储库。我已将 ~/.ssh/config 中的 Identityfile 设置为如下所示。

Host *.bitbucket.org
   IdentityFile ~/.ssh/company_id_rsa

每当我想将某些东西推送到我的个人存储库时,我都会将配置文件更改为如下所示。

Host *.bitbucket.org
   IdentityFile ~/.ssh/personal_id_rsa

现在,每当我想进行 git push 时,编辑文件变得非常不便。我只是在想,如果我可以在飞行时拿起其中一个键,当我按下时,它会容易得多。有什么办法吗?

我遇到了 this解释类似用例的问题,但这不是此处的确切用例。

最佳答案

您可以在 ssh 配置文件中添加两个 Bitbucket“帐户”。 Bitbucket 有替代的 ssh 主机监听端口 443(对于那些几乎阻止了所有端口的人(原文如此!))。

Host bitbucketCompany
    User git
    HostName altssh.bitbucket.org
    Port 443
    IdentityFile ~/.ssh/company_id_rsa

Host bitbucketWork
    User git
    HostName bitbucket.org
    Port 22
    IdentityFile ~/.ssh/personal_id_rsa

然后在 .git/config 中更新你的 Remote

公司项目

[remote "origin"]
    url = ssh://bitbucketCompany/username/repo.git

个人项目

[remote "origin"]
    url = ssh://bitbucketPersonal/username/repo.git

关于git - 对同一 Git 主机上的不同帐户使用不同的 SSH key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353564/

相关文章:

Git 在 checkout 时更改了我的文件权限

c# - 从C#(WPF)程序向putty.exe实例发送命令

windows - 通过命令行通过 SOCKS5 代理打开 putty ssh 连接

git - bitbucket ssh 连接问题 "shell request failed on channel 0."即使我可以提交这个 repo

git - bitbucket 无法从存储库中 pull/推送

git:如何从项目中分离出库?过滤器分支,子树?

bash - Git diff - 忽略重新排序

git - 有没有办法比较两个差异或补丁?

python - Airflow SSH 运算符(operator)错误,意外的关键字参数

Xcode/Git 存储库错误 : "Please verify that the working copy is reachable and try again."