同一台电脑使用不同的git账户登录不同的git网站

632 阅读2分钟

使用不同的git账户登录不同的git网站

需求

我们在一台电脑上工作的时候,会遇到一个工作人员登录多个网站的问题。举例:一个人登录外网的GitHub和gitee用于自己学习,登录自己内网布置的git用于工作。这里就需要配置不同的SSHkey来进行操作。

注意:一定要符合公司规范哦,千万别一时手滑上传公司代码到外网。

环境以及思路

环境是mac。
操作的思路是:
1 通过ssh的config文件,使用host限定ssh key
2 不同的网站使用不同的ssh

步骤

0 命令行进入制定文件位置

~/.ssh

1 生成SSH key

 ssh-keygen -t rsa -C "youremail@yourcompany.com” -f ~/.ssh/id-rsa

-C 后面是你的邮箱  -f 后面是文件的位置

2  配置config文件

touch config

在里面写

# github(meow@163.com)
Host github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
User meow@163.com

# gitee(meow2@163.com)
Host gitee
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_meow
User meow2@163.com

配置项具体的意思是:

Host ssh 连接时候的名称 例如 ssh github

HostName 域名名称

IdentityFile  位置

User 用户邮箱

PreferredAuthentications 验证顺序 具体参照 linux.die.net/man/5/ssh_c…

3 将各自的sshkey放到对应的网站

GitHub - 账户的创建和配置

4 验证ssh

ssh -T gitee

如果出现了验证成功相应字样那么恭喜你成功啦!

5 根据需要改一下工程的用户名邮箱

我们可以配置一个全局邮箱、用户,用于主要工作。另外一个在本地工程中单独配置,用于自学,也就是local。 位置在 .git/config 中

显示隐藏文件的快捷键 cmd +  shift +  . <--点

这样整个流程走完,如果需要扩展可以看一下参考文献。

参考链接

linux.die.net/man/1/ssh-k…
www.jianshu.com/p/664693379…
my.oschina.net/stefanzhlg/…
www.kbase101.com/question/43…