配置多个 GitHub 账号(SSH)
假设一个名字为aaa,一个名字为bbb
1.为每个账号生成不同的 SSH 密钥
# 为账号 aaa |
2.将公钥添加到对应 GitHub 账户
进行下述操作,得到.pub
文件内容cat ~/.ssh/id_ed25519_aaa.pub
cat ~/.ssh/id_ed25519_bbb.pub
进入各自的 GitHub 账号页面:
- 打开 GitHub → Settings → SSH and GPG keys → New SSH key
- 把你生成的
.pub
文件内容粘贴进去
3.配置 SSH 配置文件 ~/.ssh/config
打开 SSH 配置文件:notepad ~/.ssh/config
写入以下内容(每个账户一个 Host):# aaa 账号配置
Host github-aaa
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_aaa
# bbb 账号配置
Host github-bbb
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_bbb
4.项目中使用不同的 SSH 地址
假设你有两个项目分别归属于不同账号:
aaa 账号的项目使用下述git remote set-url origin git@github-aaa:aaa/aaa-project.git
bbb 账号的项目使用下述git remote set-url origin git@github-bbb:bbb/bbb-project.git
注意:此处 github-aaa
和 github-bbb
是你在 ~/.ssh/config
中定义的别名,替代了默认的 github.com
。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 森林!