配置多git账号
生成SSH密钥
ssh-keygen -t rsa -C "your email"
可以通过命名活着放在不同的目录进行区分。
添加新的SSH keys到GitHub中
将新生成的SSH keys添加到你的另一个github帐号(或者公司的gitlab)下的SSH Key中。
清除git的全局设置
如果你之前在设置本地仓库和github连接的时候设置过user.name和user.email,那么你必须首先清楚掉该设置,因为不清楚掉该设置,两个账号在提交资料的时候,验证肯定冲突(只能设置一个全局的user.name和user.email,而你现在有两个账号就对应两个不同的)。
git config --global user.name "your_name"
git config --global user.email "your_email"
执行ssh-agent让ssh识别新的私钥
分别使用ssh-add
将其添加到ssh-agent中。
ssh-add ~/.ssh/id_rsa
注:直接执行ssh-add
是临时的,可以执行ssh-add -K
将其永久添加
如果出现Could not open a connection to your authentication agent
的错误,可以先执行一遍ssh-agent bash
,之后在执行ssh-add
。
配置config文件
看下~/.ssh/
目录下有没有config,没有直接新建一个touch config
,有的话就直接在后边添加配置。
配置如下:
# 第一个
Host git@one.github.com
HostName https://github.com
User git
IdentityFile ~/.ssh/one/id_rsa
# 第二个
Host git@two.github.com
HostName https://github.com
User git
IdentityFile ~/.ssh/two/id_rsa
# ....第N个
测试
ssh -T git@one.github.com
ssh -T git@two.github.com
clone Git仓库
假设原来的git ssh 地址是git clone git@xxxxxx.com:xxx/test.git
那么新的地址就对应配置config时候不同的密钥对应的HostName,
比如是上边的one,那么,新地址就应该是git clone git@one.xxxxxx.com:xxx/test.git
当然,如果懒得话,不改地址也是可以的,同样可以正常的操作,但是在push到仓库后,可以看见多个头像。