豁然高

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

git 同一台机器上管理多个github账户

同一台机器上一个ssh key只能管理一个github账户,
如果给第二个github账户添加同一个ssh key时会提示以下错误

Key is already in use

 

所以要想在同一台机器上管理多个github账户,那就需要使用多个ssh key

1.创建一个新的ssh key

ssh-keygen -t rsa -C "youreothermail@example.com" -f ~/.ssh/id_rsa_work

如果没有 -f 选项,那么默认生成的文件为id_rsa和id_rsa.pub
使用 -f 选项可以指定生长的ssh key 的文件名,上面的例子就会生成 id_rsa_work和id_rsa_work.pub

 

创建一个新的ssh key后,..ssh/文件夹中就有以下两对ssh key

id_rsa
id_rsa.pub
id_rsa_work
id_rsa_work.pub

 

2.配置 SSH 配置文件

~/.ssh/ 目录下创建或编辑 config 文件,配置每个 GitHub 账户对应的 SSH 密钥。示例配置如下:

复制代码
# GitHub account 1
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# GitHub account 2
Host github.work
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_work
复制代码

github.com和github.work是github.com的别名

 

3. 分别将id_rsa.pub和id_rsa_work.pub添加到不同的github账户中,比如以下两个账户

git@github.com:accout1
git@github.com:accout2

4. 从远程库clone到本地或者将本地库关联到远程库

从远程库clone到本地

git clone github.com:account1/repo1.git
git clone github.work:account2/repo2.git

将本地库关联到远程库

git remote add origin git@github.com:account1/repo1.git
git remote add origin git@github.work:account2/repo2.git

使用别名时就会在.ssh/config文件中找到别名对应的IdentityFile(秘密键文件),从而与对应的github账户中添加的公开键进行认证

 

posted on   豁然高  阅读(133)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示