一台电脑上的git同时使用两个github帐户

 

需求: linux

公司有github帐号,本身有github帐号,想在git上同时使用,二者互不干扰。git

 

思路: github

管理两个SHH key。bash

 

解决方案:ssh

 

1、生成两个SSH key测试

 

为了举例方便,这里使用“one”和“two”两个帐户。下同。spa

 

$ ssh-keygen -t rsa -C "one@gmail.com"

$ ssh-keygen -t rsa -C "two@gmail.com"

 

不要一路回车,分别在第一个对话的时候输入重命名(id_rsa_oneid_rsa_two),这样会生成两份包含私钥和公钥的4个文件。命令行

 

注1:ssh-keygen是linux命令,可让两个机器之间使用ssh而不须要用户名和密码 code

住2:必定要在~/.ssh路径下运行命令行,否则生成的文件不会出如今当前目录blog

 

2、添加私钥

 

一、打开ssh-agent


(1)若是你是github官方的bash:

$ ssh-agent -s

(2) 若是你是其它,好比msysgit:

$ eval $(ssh-agent -s)

 

二、添加私钥

$ ssh-add ~/.ssh/id_rsa_one

$ ssh-add ~/.ssh/id_rsa_two

 

3、建立config文件

 

$ touch config

 

此时会出现空的config文件,而后添加以下内容:

 

# one(one@gmail.com)

    Host one.github.com

  HostName github.com

  PreferredAuthentications publickey

  IdentityFile ~/.ssh/id_rsa_one

  User one

 

# two(two@ gmail.com)

    Host two.github.com

  HostName github.com

  PreferredAuthentications publickey

  IdentityFile ~/.ssh/id_rsa_two

  User two

 

4、部署SSH key

 

分别登录两个github帐号,进入Personal settings –> SSH and GPG keys

 

点击"new SSH key", 把下面两个公钥的内容分别添加到相应的github帐号中。

  

5、远程测试【可跳过】

 

$ ssh –T one.github.com

$ ssh –T two.github.com

 

6、使用

 

一、clone到本地

 

(1)原来的写法:

$ git clone git@github.com: one的用户名/learngit.git

(2)如今的写法:

$ git clone git@one.github.com: one的用户名/learngit.git

$ git clone git@two.github.com: two的用户名/learngit.git

 

二、记得给这个仓库设置局部的用户名和邮箱:

$ git config user.name "one_name" ; git config user.email "one_email"

$ git config user.name "two_name" ; git config user.email "two_email"

 

三、上述都成功后,会发现钥匙会由灰变绿。

相关文章
相关标签/搜索