应用收藏-工具篇

程序员必学必会- Git 设置多个账号

2019-03-22  本文已影响0人  ifjgm

概述

很多时候我们会有 Github 的账号,公司的 GitLab 账号(或者其他类型的 Git 账号),那我们就有多个Git 账号了,如何在一个终端中配置多个Git 账号呢?

一:清除原有全局设置

 git config --global user.name 'xxxxx' 
 git config --global user.email 'xxxx' 

设置过全局用户名、email 等信息,则不用清除原有设置。(当然如果你也记不得了,那就这么操作下吧)

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

二:生成ssh 密钥

mkdir company
mkdir github
ssh-keygen -t rsa -C "your_gitlab_email@xxx.com"
Enter file in which to save the key (/Users/tuoanlan/.ssh/id_rsa):
 company/id_rsa_company
Enter passphrase (empty for no passphrase):
Your identification has been saved in company/id_rsrsa_company.
Your public key has been saved in company/id_rsrsa_company.pub.

说明ssh 密钥已经生成并保存在 company 文件夹下 id_rsa_companyid_rsa_company.pub

二:将公钥添加到网站

pbcopy <id_rsa_company.pub

配置

# The git info for company
Host qdjr.git.zhudb.com                 # 写公司git 地址
HostName qdjr.git.zhudb.com             # 写公司git 地址
User git                                # 可以写邮箱名称,也可以写 git 账号
IdentityFile ~/.ssh/company/id_rsa      #对应公司 git 账号密钥路径,注意不要写错

# The git info for github           
Host github.com                             # 写 github 的地址
HostName github.com                         # 写 github 的地址
User git                                    # 可以写邮箱名称,也可以写 git 账号
IdentityFile ~/.ssh/github/id_rsa_github    # 对应github 密钥路径,注意不要写错


以上内容只是示例,如果想直接复制后修改,请复制如下内容到 config

# The git info for company
Host  qdjr.git.zhudb.com
HostName  qdjr.git.zhudb.com
User zhangyg@baihang-china.com
IdentityFile ~/.ssh/company/id_rsa_company

# The git info for github
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/github/id_rsa

把专用密钥添加到 ssh-agent 的高速缓存中

执行命令 ssh-addIdentityFile 添加到 ssh-agent高速缓存中,执行如下命令

 ssh-add ~/.ssh/company/id_rsa_company 
 ssh-add ~/.ssh/github/id_rsa

验证是否配置成功

 ssh -T git@qdjr.git.zhudb.com
The authenticity of host 'qdjr.git.zhudb.com (121.43.184.183)' can't be established.
ECDSA key fingerprint is SHA256:BGEoBwmuXjA3mzMGnU2dxvpdlPv8pxBDZgA3SKOFMVs.
Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added 'qdjr.git.zhudb.com,121.43.184.183' (ECDSA) to the list of known hosts.
Welcome to GitLab, xxx!

总结

上一篇下一篇

猜你喜欢

热点阅读