一台电脑上使用两个GitHub账户

本文详细介绍了如何使用ssh-keygen生成并配置多个SSH密钥,以实现同一台计算机上通过不同的GitHub账号进行Git操作。通过修改~/.ssh/config文件和项目.git/config文件,可以指定特定的SSH密钥用于不同的GitHub账号,从而避免了频繁切换SSH密钥的麻烦。

使用ssh-keygen生成两个ssh key,目录内容如下:

$ ll ~/.ssh/
total 15
-rw-r--r-- 1 Cui 197121  336 九月 15 10:25 config
-rw-r--r-- 1 Cui 197121 1675 七月 19 22:56 id_rsa
-rw-r--r-- 1 Cui 197121  398 七月 19 22:56 id_rsa.pub
-rw-r--r-- 1 Cui 197121 1675 九月 15 00:09 id_rsa_other
-rw-r--r-- 1 Cui 197121  398 九月 15 00:09 id_rsa_other.pub
-rw-r--r-- 1 Cui 197121 1989 九月 12 21:57 known_hosts

ssh的config文件如下:

$ cat ~/.ssh/config
# one

    Host github.com

    HostName github.com

    PreferredAuthentications publickey

    IdentityFile ~/.ssh/id_rsa

    User legend9207

# two

    #远程主机地址别名;git config中远程git服务地址可以配置成这个,以达到相同git服务地址使用不同ssh key的目的。
    Host cuiyindeng.github.com

    HostName github.com

    PreferredAuthentications publickey

    IdentityFile ~/.ssh/id_rsa_other

    User cuiyindeng

两个项目的git config文件如下:

项目1目录/.git/config
...
[remote "origin"]
	url = git@github.com:legend9207/legend9207.github.io.git
	fetch = +refs/heads/*:refs/remotes/origin/*
...

项目2目录/.git/config
...
[remote "origin"]
    #此时的git服务地址用的是ssh config中的别名。
	url = git@cuiyindeng.github.com:cuiyindeng/cuiyindeng.github.io.git
	fetch = +refs/heads/*:refs/remotes/origin/*
...

参考文章:https://blog.youkuaiyun.com/wolfking0608/article/details/78512171

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值