清除全局git配置信息
git config --global --unset user.name
git config --global --unset user.email
查询
git config --global user.name
git config --global user.email
生成github密钥
进入目录,该目录下保存生成的秘钥
cd ~/.ssh
ssh-keygen -t rsa -C "youremail"
生成新的密钥提示
输入新文件名称
出现密码提示直接enter,如果设置密码了,git push或者gull 都要输入密码
出现如下提示则生成成功
github上添加公钥配置
cat id_rsa_github.pub 复制公钥添加到github
配置config
在.ssh目录下新建一个config文件
touch config
vim config复制下面配置
Host github.com // 网站的别名,随意取
HostName github.com // 托管网站的域名
User cornfower // 随意取名字
IdentityFile ~/.ssh/id_rsa_github // 使用的密钥文件
// GitLab的配置相同
Host gitlab.com
HostName gitlab.com
User cornfower
IdentityFile ~/.ssh/id_rsa_gitlab
验证
ssh -T git@github.com
提交账户用户名邮箱切换
配置好后,commit代码时出现
需要在当前目录配置
git config user.name "yourname"
git config user.email "youremail"
也可以配置全局因为有两个账户用户名邮箱不一样(一样的可忽略)所以需要
vim ~/.bash_profile添加如下配置别名方便切换账户
alias cgc='git config --global user.name 用户名1 && git config --global user.email 邮箱1 && git config user.name && git config user.email'
alias cgg='git config --global user.name 用户名2 && git config --global user.email 邮箱2 && git config user.name && git config user.email'
保存退出
执行source ~/.bash_profile ,更新配置
执行命令切换账户