简体   繁体   English

我可以使用两个不同的 Git 和它们各自的 SSH 密钥吗?

[英]Can I use two different Git with their respective SSH Key?

I have two different git accounts.我有两个不同的 git 帐户。

Account 1 GitHub githubaccount@email.com账号 1 GitHub githubaccount@email.com

Account 2 GitLab gitlubaccount@email.com帐户 2 GitLab gitlubaccount@email.com

In my .ssh folder I have a SSH Key from my GitHub account.在我的 .ssh 文件夹中,我有一个来自我的 GitHub 帐户的 SSH 密钥。 In the config file I have在我的配置文件中

Host *
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/id_edxxxxx

In my git config --global user.mail and name I have the GitLab account information.在我的 git config --global user.mail 和名称中,我有 GitLab 帐户信息。

My question is the following, how can I use both accounts with their respective SSH keys for different projects?我的问题如下,如何将两个帐户及其各自的 SSH 密钥用于不同的项目? Is it possible?可能吗?

I suggest generating a single SSH key and adding the public key it to both your GitLab and GitHub accounts.我建议生成一个 SSH 密钥并将其添加到您的 GitLab 和 GitHub 帐户中。 The emails you set for each local repo don't matter.您为每个本地存储库设置的电子邮件无关紧要。 These are only used to set on commits and have nothing to do with authentication with remote services.这些仅用于设置提交,与远程服务的身份验证无关。

In my git config --global user.mail and name I have the GitLab account information.在我的git config --global user.mailname中,我有 GitLab 帐户信息。

This has nothing to do with authentication: you could have xxx and xxx@yyy.com , that would be the same.这与身份验证无关:您可以拥有xxxxxx@yyy.com ,这将是相同的。

how can I use both accounts with their respective SSH keys for different projects?如何将两个帐户及其各自的 SSH 密钥用于不同的项目?

That means two key:这意味着两个关键:

  • the default one ~/.ssh/id_edxxxxx : its public key should be registered to user1默认一个~/.ssh/id_edxxxxx :它的公钥应该注册到 user1
  • ssh-keygen -t ed25519 -P "" -f ~/.ssh/gh2 : the gh2.pub should be regered to user2. ssh-keygen -t ed25519 -P "" -f ~/.ssh/gh2 : gh2.pub 应该注册给 user2。

From there, add a ~/.ssh/config file with从那里,添加一个 ~/.ssh/config 文件

Host gh1
User git
Port 22
Hostname github.com
IdentityFile ~/.ssh/id_ed25519
TCPKeepAlive yes
IdentitiesOnly yes

Host gh2
User git
Port 22
Hostname github.com
IdentityFile ~/.ssh/gh2
TCPKeepAlive yes
IdentitiesOnly yes

Replace your remote URL by:将您的远程 URL 替换为:

cd /path/to/project1/local/clone
git remote set-url origin gh1:User1/project1

cd /path/to/project2/local/clone
git remote set-url origin gh2:User2/project2

That way, "both accounts with their respective SSH keys for different projects".这样,“两个帐户都具有各自用于不同项目的 SSH 密钥”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM