环境 macos
检查是否已有全局账户设置
$ git config --global user.name $ git config --global user.email
如果都没有返回值,就说明没有已配置的 git 账户,否则请删除账户信息:
$ git config --global --unset user.name "yourname" $ git config --global --unset user.email "youremail"
生成公钥和秘钥
$ ssh-keygen -t rsa -c "your_email1" $ ssh-keygen -t rsa -c "your_email2"
注意
生成过程中的 enter file in which to save the key (/users/faremax/.ssh/id_rsa):
一步骤请分别起不同的名。
查看生成的文件
ls -l ~/.ssh
-rw------- 1 faremax staff 1679 7 6 11:05 id_rsa_github -rw-r--r-- 1 faremax staff 400 7 6 11:05 id_rsa_github.pub -rw------- 1 faremax staff 1679 7 6 11:06 id_rsa_gitlab -rw-r--r-- 1 faremax staff 405 7 6 11:06 id_rsa_gitlab.pub -rw-r--r-- 1 faremax staff 602 12 6 2017 known_hosts
忽略2017年创建的几个文件,可以看到两个不同的公钥和私钥对已经生成成功了。
分别在 github 和 gitlab 中录入对应的公钥
$ pbcopy < id_rsa_github.pub # 复制文件内容
$ pbcopy < id_rsa_gitlab.pub
添加并识别私钥
$ ssh-agent bash $ ssh-add ~/.ssh/id_rsa $ ssh-add ~/.ssh/id_rsa_github $ ssh-add ~/.ssh/id_rsa_gitlab
- 创建配置文件
$ touch ~/.ssh/config
在该文件中添加以下配置
#该文件用于配置私钥对应的服务器 #default gitlab user(email1@hostname.com) host git@hostname.com hostname https://hostname.com user git identityfile ~/.ssh/id_rsa_gitlab ###################################### #add github user(email1@hostname.com) host git@github.com hostname https://github.com user git identityfile ~/.ssh/id_rsa_github
验证连接git
以下输入输出表示链接成功,如果提示权限问题,说明秘钥和公钥匹配除了问题,请检查并重复上述步骤(使用 -vt
参数执行下面命令查看错误信息)
$ ssh -t git@github.com hi faremax! you've successfully authenticated, but github does not provide shell access. $ ssh -t git@hostname.com welcome to git, faremax!
以上就是git 多账户配置完整实现过程详解的详细内容,更多关于git 多账户配置的资料请关注代码网其它相关文章!
发表评论