本地管理多个git账号

前言

本来电脑本地只存在一个git账号,用于提交项目代码。近期重新搭建了博客,站点托管在github上。在gitee上又新建了一个仓库,用来备份博客。首次提交到github或者gitee上,会提示输入密码,再次提交,可能不再提示密码,当你做push操作时,会提示你没有权限,因为git无法区分到底是用哪个账号进行操作。

如果能够通过一个文件来管理多个账号,并且根据不同的仓库来自动选择不同的账号来执行git操作,那将会是一件多么愉快的事。下面我们就来完成这件愉快的事。

配置SSH密钥

这里涉及到三个git账号,公司git账号:company_account,博客站点git账号:website_account,博客备份git账号:blog_account

1.分别生成company_account,website_account,blog_account对应的rsa和rsa.pub文件

ssh-keygen -t rsa -C "company_account"

默认会放在/Users/UserName/.ssh路径下,默认文件名为id_rsa,为了区分,改文件名为company_id_rsa。回车后,会提示设置密码。

同样生成website_account和blog_account,分别改文件名为website_id_rsa和blog_id_rsa,操作完后,在.ssh文件夹中会有这样6个文件

company_id_rsa

company_id_rsa.pub,

website_id_rsa

website_id_rsa.pub,

blog_id_rsa

blog_id_rsa.pub

2.分别把company_id_rsa.pub,website_id_rsa.pub,blog_id_rsa.pub里的内容复制到公司、github、gitee的ssh里面

配置config文件

vim ~/.ssh/config

参考如下:

#compay

Host xxxxx.com

HostName xxxxx.com

User compay_account

IdentityFile ~/.ssh/company_id_rsa

#website

Host github.com

HostName github.com

User website_account

IdentityFile ~/.ssh/website_id_rsa

#blog

Host gitee.com

HostName gitee.com

User blog_account

IdentityFile ~/.ssh/blog_id_rsa

Host: 服务器

HostName: 主机名或域名,建议使用域名

User: 用户名或者邮箱

IdentityFile: rsa文件路径

后续使用git,会通过config文件来自动选择账号。

————————————————

原文链接:https://blog.csdn.net/walkstep/article/details/84824512

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容