Git多账户配置

日常开发过程中,我们可能遇到需要在同一电脑上配置多个Git账户的情况;github、公司的git服务器等,这时候我们需要配置多个ssh。以Mac为例。

1.取消全局的账户配置

查看全局账户

git config --global user.name

如果存在,取消全局设置

 git config --global --unset user.name
 git config --global --unset user.email  

2.创建不同user的ssh key

# 新建ssh key 
cd ~/.ssh 
ssh-keygen -t rsa -C "new_user@email.com"  # 生成新的ssh key
# 设置新的ssh key的名称
Enter file in which to save the key (/Users/{username}/.ssh/id_rsa): id_rsa_oschn

3.新密钥添加到ssh agent中

默认只添加了id_rsa,因此需要将新的ssh key 添加

ssh-add ~/.ssh/id_rsa_oschn

如果添加报错:Could not open a connection to your authentication agent,尝试以下命令

ssh-agent bash
ssh-add ~/.ssh/id_rsa_oschn

4.修改config文件

在~/.ssh目录下找到config文件,如果没有就创建

touch config #创建config文件

修改config 文件

# 该文件用于配置私钥对应的服务器
# corporation gitlib user(xxx@corp.com.cn)
Host git@git.corpxxx.com
HostName https://git.corpxxx.com
User git
IdentityFile ~/.ssh/id_rsa

# second user(user@xxx.com)
# OSChina
Host git@gitee.com
HostName https://gitee.com
User git
# PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_oschn

5.测试

ssh -T git@gitee.com

6.其他

在使用idea的时候,可能出现命令行可以clone 代码,但是idea里面不能clone代码的情况,检查一下配置:
Version Control -> Git -> SSH executable: Native

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

推荐阅读更多精彩内容

  • 在使用git的时候,一般我们使用的远程Git服务器是github,这时只需简单的生成ssh-key密钥对并将公钥添...
    gofanelena阅读 5,607评论 0 5
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,845评论 18 139
  • 安得万里风,飘飖吹我裳。唐 杜甫 《夏夜叹》 摘要 目前使用 git 作为文件版本控制工具的开发者越来越多,同时一...
    FosterDylan阅读 4,035评论 1 4
  • 北方 所有的窗户都醉了 含情脉脉地凝视 南方 飘舞的衣裙 燃烧的楼道拐角 有眷恋碾压的声音 西方的白色灯光对夕阳冷...
    蟋蟀王阅读 155评论 0 1
  • 本文主要实现主题换肤的管理以及两个常用控件的封装 大致思路: 1.建立一个单例类(ThemeManager)专门用...
    fuxi阅读 838评论 0 2