美文网首页
git 多账户配置

git 多账户配置

作者: faremax | 来源:发表于2018-07-17 15:21 被阅读9次

环境 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   # 复制文件内容
image
$ pbcopy < id_rsa_gitlab.pub
image
  • 添加并识别私钥
$ 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配置 Git最小配置 配置全局账户,该账户对所有Git仓库都有效 配置局部账户,该账户对当前Git仓库有效 ...

  • Git 葵花宝典

    账户配置 配置全局账户,对所有 Git 仓库有效 配置局部账户,只对当前 Git 仓库有效 查看全局配置 查看局部...

  • git 多账户配置

    1、生成多个密钥 ssh-keygen -t rsa -C "邮箱" -f 文件名 2、在 .ssh 目录下创建 ...

  • git 多账户配置

    环境 MacOS 检查是否已有全局账户设置: 如果都没有返回值,就说明没有已配置的 git 账户,否则请删除账户信...

  • Git多账户配置

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

  • git 多账户配置

    在Git使用中经常会碰到多用户问题,例如:你在公司里有一个git账户,在github上有一个账户,并且你想在一台电...

  • GIT快速入门

    基本配置 下载软件 git-scm Git配置用户名与邮箱 配置公钥 多账户配置(可跳过) 传送门: Window...

  • 本地配置Git多账户

    最近遇到这样一种情况,在一台电脑上需要设置两个git账号。两个账号都有其作用。所以记录一下设置多账号的过程 1. ...

  • Git多账户配置教程

    安装Git后,就可以进行配置:用户名,邮箱,SSH密钥等。也许你有多个Git账号,分别对应github.com、c...

  • Git多账户切换配置

    1、手动切换 gitlab和github项目切换时, github上多个项目账户不同时,都需要切换用户。 但是这个...

网友评论

      本文标题:git 多账户配置

      本文链接:https://www.haomeiwen.com/subject/koxgpftx.html