如何在一台电脑上设置多个github账号
背景:一台PC需要配置两个不同的GitHub账号,实现ssh的连接并push到不同账号下的仓库。
第一步:生成密钥
- 执行: ssh-keygen -t rsa -C “fuwenhao945@gmail.com“
- 可以指定生成文件命名: /Users/fwh/.ssh/id_rsa_github_gmail
- 添加私钥: ssh-add id_rsa_github_gmail
- 同上步骤生成另一个账号的公钥:
- ssh-keygen -t rsa -C “fuwenhao594@163.com“
- /Users/fwh/.ssh/id_rsa_github_163_fwh666
- ssh-add id_rsa_github_163_fwh666
第二步:将密钥配置到Github账户中
- 登录Github上,选择个人设置(setting)
- 选择SSH and GPG keys 选项
- New SSH keys
- 输入公钥,即生成的id_rsa_github_gmail.pub的文件内容
- 标题自定义
第三步: 编辑配置文件
在/Users/fwh/.ssh目录下创建文件config
输入内容:
# github配置-gmail
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github_gmail
#github配置-163-fwh666
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github_163_fwh666
第四步:测试验证
- 输入: ssh -T git@github.com
- 返回: Hi wenhaoclub! You’ve successfully authenticated, but GitHub does not provide shell access.
- 账号对应的用户名称一致,表示成功。
- 输入: ssh -T git@gitee.com
- 返回正常信息
参考地址:
- 一台电脑上同时使用两个github账户 https://www.programminghunter.com/article/6112809308/
- 执行ssh-add时添加私钥到git中报错Could not open a connection to your authentication agent https://blog.csdn.net/Dior_wjy/article/details/79035214