如何在同一台电脑上使用两个github账户(亲测有效)

1 前言

由于有两个github账号,要在同一台电脑上同步代码,需要给每一个账号添加一个SSH public key,此时推送时git push origin,不知道是哪个账号的远程仓库名称,所以需要配置一下~/.ssh下面的config信息,否则会自动使用.ssh/id_rsa.pub所对应的账户进行登陆并操作。

若没配置,提示的错误信息如下:

$ git push
ERROR: Permission to B/test2.git denied to A.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.  

2 步骤

解决方案(假设你已经拥有私有账号且已经OK,现在想使用另一个工作用账号):

2.1 为工作账号生成SSH Key(先切换到 ~/.ssh下),具体参照Gitlab的SSH配置

ssh-keygen -t rsa -C your.email@example.com -b 4096                                                                                                                                                                                          

存储key的时候,不要覆盖现有的id_rsa,使用一个新的名字,比如id_rsa_work

2.2 配置.ssh/config

$ vi .ssh/config

# 加上以下内容
#default github
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa

Host github_work
HostName github.com
IdentityFile ~/.ssh/id_rsa_work

2.3 git push remote add origin2  

#push到github上去
$ git remote add origin2 git@github_work:B/test2.git

2.4 id_rsa_work.pub加到你的work账号上

$ git remote -v
origin  git@github.com:B/test2.git (fetch)
origin  git@github.com:B/test2.git (push)
origin2 git@github_work:B/test2.git (fetch)
origin2 git@github_work:B/test2.git (push)

这样就完成配置了。

2.5 git push origin2

git push origin2 master 

你就可以通过使用github.com别名github_work来明确说你要是使用id_rsa_work的SSH key来连接github,即使用工作账号进行操作。


3 参考:

1. 如何在同一台电脑上使用两个github账户

转载于:https://www.cnblogs.com/fanbi/p/10185587.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值