码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-22 17:04:42      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:ace   known   清空   表示   文本   config   tar   host   如何   

1. 新建.ssh目录

 

 

2. 生成并添加第一个ssh key

$ ssh-keygen -t rsa -C "youremail@yourcompany.com" -f id_rsa_A

Git Bash中执行这条命令一路回车,会在 ~/.ssh/ 目录下生成 id_rsa_A 和 id_rsa_A.pub 两个文件,用文本编辑器将 id_rsa_A.pub 中的内容复制一下粘贴到账户A的github上

 

 

3. 生成并添加第二个ssh key

$ ssh-keygen -t rsa -C "youremail@yourcompany.com" -f id_rsa_B

Git Bash中执行这条命令一路回车,会在 ~/.ssh/ 目录下生成 id_rsa_B 和 id_rsa_B.pub 两个文件,用文本编辑器将 id_rsa_B.pub 中的内容复制一下粘贴到账户B的github上

 

 

4. 添加私钥

在 ~/.ssh 目录下新建一个known_hosts文件

$ touch known_hosts

 

git自动把新生成的私钥写到known_hosts中

$ ssh-add ~/.ssh/id_rsa_A
$ ssh-add ~/.ssh/id_rsa_B

 

如果执行ssh-add时提示"Could not open a connection to your authentication agent",可以现执行命令:

$ ssh-agent bash

 

然后再运行ssh-add命令

# 可以通过 ssh-add -l 来确私钥列表
$ ssh-add -l

# 可以通过 ssh-add -D 来清空私钥列表
$ ssh-add -D

 

 

5. 修改配置文件

在 ~/.ssh 目录下新建一个config文件

$ touch config

 

添加内容:(此处新加你的私钥目录和私钥对应的HostName)

# github
Host github_A
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_A

# github
Host github_B
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_B

这样的话,你就可以通过使用github.com别名github_A(github_B)来明确说你是要使用id_rsa_A(id_rsa_B)的SSH key来连接github,即账号A(账号B)进行操作。

以后clone或者push时,git@github.com:xxxx/test.git   =>   git@github_A:xxxx/test.git或者git@github_B:xxxx/test.git

 

 

6. 测试

$ ssh -T git@github_A

或者

$ ssh -T git@github_B

输出:"Hi user! You‘ve successfully authenticated, but GitHub does not provide shell access. ",就表示成功的连上github了

 

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

标签:ace   known   清空   表示   文本   config   tar   host   如何   

原文地址:https://www.cnblogs.com/softwarefang/p/9350296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!