如何设置多个Git帐号

1. 配置不同的用户名和邮箱

1.1 介绍

  Git共有三个级别的config文件,分别是system、global和local。global的在$home\.gitconfig,local的在仓库目录下的.git\config。这三个级别都分别配置了用户信息,当git commit时,会依次从local、global、system里读取用户信息。因此,我们利用local的配置来设置不同的用户信息

1.2 查看

  可以使用以下命令查看用户信息

git config user.name
git config user.email

1.3 更改

  在仓库目录下,使用以下命令设置local级别的用户信息,只对当前仓库生效

git config --local user.name LemonCat
git config --local user.email lemoncat@***.com

2. 配置不同的SSH秘钥

2.1 生成秘钥

  使用以下命令生成秘钥。注意,需要指定目录及文件名,避免覆盖。-C后面的字符串为标识用,可自定义

ssh-keygen -t rsa -b 4096 -f C:/Users/BeiTa/.ssh/github -C "lemoncat@***.com"

  或者到用户目录下的.ssh文件夹内执行以下第一行命令,并在第三行提示时指定文件名

$ ssh-keygen -t rsa -b 4096 -C "lemoncat@***.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/BeiTa/.ssh/id_rsa): 1
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in 1.
Your public key has been saved in 1.pub.

2.2 上传秘钥

  以github为例,依次点击Setting->SSH and GPB keys->New SSH Key,复制粘贴生成的.pub文件中的秘钥

2.3 修改SSH配置

  在用户目录下的.ssh文件夹下输入以下指令开启ssh代理

ssh-agent bash

  继续输入以下命令添加秘钥

$ ssh-add ~/.ssh/github

  核对地址是否正确

Identity added: /c/Users/BeiTa/.ssh/github (/c/Users/BeiTa/.ssh/github)

  在此目录下创建config文件,文件名没有后缀,加入以下配置

Host alias
User lemoncat
HostName github.com
IdentityFile ~/.ssh/github

Host github.com
User lemoncat2
HostName github.com
IdentityFile ~/.ssh/id_rsa

  Host为别名,可以自己设置。设置后SSH地址git@github.com:lemoncat/test.git中的github.com需要替换成别名来使用,如git@alias:lemoncat/test.git

3. 参考文章

作者:脑袋Mini
链接:https://www.jianshu.com/p/0ad…

Tips

指定ssh秘钥的bat命令:

set rsa_path=operation/keygen/id_rsa
git config core.sshCommand "ssh -i %rsa_path% "
    原文作者:像猫一样待着
    原文地址: https://segmentfault.com/a/1190000016302881
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞