GitHub-Multiple-Account

因为linux下面一个用户只拥有一个本身的公钥和私钥,因此没法同时在两个不一样的GitHub帐号上面设置相同的ssh key,因此最简单的办法就是新建一个用户,而后这个新建的用户生成本身的ssh key。可是还有另一种更加优雅的方式。linux

解决方案

$ ls ~/.ssh
authorized_keys  id_rsa  id_rsa.pub  known_hosts

1.其中id_rsaid_rsa.pub分别是对应的私钥和公玥,由于要配置两个帐号,因此咱们还须要生成另一对:git

$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_2 -C "fsmro@163.com"

2.而后从新在~/.ssh下面新建一个config文件,添加下面的内容:github

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host gitlab.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_2
$ ls ~/.ssh
authorized_keys  config  id_rsa_2  id_rsa_2.pub  id_rsa  id_rsa.pub  known_hosts

3.而后将公钥添加到GitHub帐号shell

$ sudo apt-get install xclip

$ xclip -sel clip < ~/.ssh/id_rsa_2.pub
# Copies the contents of the id_rsa.pub_2 file to your clipboard

4.测试一下链接:ssh

$ ssh -T git@github.com
Hi ormsf! You've successfully authenticated, but GitHub does not provide shell access.

$ ssh -T git@gitlab.com 
localhost :: ~/.ssh » ssh -T git@gitlab.com
Hi fsmro! You've successfully authenticated, but GitHub does not provide shell access.

btw: 下面这一步好像也能够不用设置,push的时候会自动识别,不肯定。ide

5.在项目下进行以下设置,取消全局设置,固然也能够保留一个全局的设置,之后不须要使用全局设置再从新设置。gitlab

# 取消全局设置
git config --global --unset user.name
git config --global --unset user.email

git config user.email “sform@163.com”
git config user.name “sform”

# 或者
git config user.email “ormsf@163.com”
git config user.name “ormsf”

或者直接编辑~/.gitconfig修改也行测试

$ cat ~/.gitconfig 
[user]
    name = ormsf
    email = ormsf@163.com
[push]
    default = simple
[http]
[http]
[http]
    proxy = http://localhost:8118
相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息