windows git多用户配置
一.什么情况下会用到多用户
比如公司有私有仓库,比如gitlab、gitea等,需要一个用户提交代码;平时自己写的小玩意传到github、或者gitee,而这俩用户又不一样。此时用git的全局配置满足不了我们需求,就要考虑多用户配置。
二.配置过程
本文将配置 github 以及 gitlab 上的两个用户,如下:
平台 | 用户名 | 邮箱 |
---|---|---|
GitHub | fcmmy | 123456@163.com |
gitlab | gitlabuser | gitlabuser@ttt.com |
1.清除全局配置
在gitbash中操作,清除全局配置
git config --global --unset user.name
git config --global --unset user.email
2.生成密钥对
密钥对的保存位置默认在 ~/.ssh (C:\Users{用户名}.ssh)目录下,我们先清理目录中已存在的密钥对信息;
生成 github 上的仓库钥对,通过 -C 参数填写 github 的邮箱:按下回车后会提示以下内容,密钥默认名称为id_rsa,在此处可以重命名,重命名为id_rsa_github
$ ssh-keygen -t rsa -C "23456@163.com"
# 按下回车后会提示以下内容,密钥默认名称为id_rsa,在此处可以重命名,重命名为`id_rsa_github`
Generatingpublic/privatersa key pair.Enter fileinwhich to save the key (/c/Users/ThinkPad/.ssh/id_rsa): id_rsa_github
同样生成gitlab的密钥对,将密钥命名为id_rsa_gitlab
$ ssh-keygen -t rsa -C "23456@163.com"
# 按下回车后会提示以下内容,密钥默认名称为id_rsa,在此处可以重命名,重命名为`id_rsa_github`
Generatingpublic/privatersa key pair.Enter fileinwhich to save the key (/c/Users/ThinkPad/.ssh/id_rsa): id_rsa_gitlab
此时.ssh目录下会生成以下文件
# 切换到.ssh目录
$ cd ~/.ssh
$ ls -li
19140298416425754 -rw-r--r-- 1 ThinkPad 197121 2602 Sep 15 10:29 id_rsa_github
48132221017636290 -rw-r--r-- 1 ThinkPad 197121 571 Sep 15 10:29 id_rsa_github.pub
21392098230126368 -rw-r--r-- 1 ThinkPad 197121 2622 Sep 15 10:29 id_rsa_gitlab
24206847997238807 -rw-r--r-- 1 ThinkPad 197121 581 Sep 15 10:29 id_rsa_gitlab.pub
3.添加公钥
分别添加公钥到github和gitlab的SSH Keys,这部分应该都会,不赘述了。
4. 添加私钥
看到以下输出即添加成功
$ ssh-add ~/.ssh/id_rsa_github
Identity added: /c/Users/ThinkPad/.ssh/id_rsa_github (23456@163.com)
$ ssh-add ~/.ssh/id_rsa_gitlab
Identity added: /c/Users/ThinkPad/.ssh/id_rsa_gitlab (gitlabuser@ttt.com)
可以通过ssh-add -l
查看已经添加的密钥
$ ssh-add -l
3072 SHA256:U/cL5tiHg/qCm5BdI26XM8hUhgKwff/Pau7TwJ8Xzts 23456@163.com (RSA)
3072 SHA256:JA8Bl/EtmDCRTQw9vd1EF9Cz8273wcO7WXKw7b2bNo4 gitlabuser@ttt.com (RSA)
注意:
如果在执行命令时可能会报以下错误,是因为git默认不会自动启动git-agent,需要手动启动,但手动启动后只能在当前bash窗口中生效,如何永久生效,后边给答案。
$ ssh-add ~/.ssh/id_rsa_github
Could not open a connection to your authentication agent.
# 执行以下命令启动git-agent,然后在添加私钥就可以了,看到以下输出即添加成功。
$ ssh-agent bash
$ ssh-add ~/.ssh/id_rsa_github
Identity added: /c/Users/ThinkPad/.ssh/id_rsa_github (23456@163.com)
$ ssh-add ~/.ssh/id_rsa_gitlab
Identity added: /c/Users/ThinkPad/.ssh/id_rsa_gitlab (gitlabuser@ttt.com)
5.管理密钥
通过以上步骤,公钥、密钥分别被添加到 git 服务器和本地了。下面我们需要在本地创建一个密钥配置文件,通过该文件,实现根据仓库的 remote 链接地址自动选择合适的私钥。
编辑 ~/.ssh(C:\Users{用户名}.ssh) 目录下的 config 文件,如果没有,请创建,名字就是config不需要拓展名。
内容如下
Host github.com
HostName github.com
User fcmmy
IdentityFile /c/Users/ThinkPad/.ssh/id_rsa_github
Host gitlab
HostName gitlab.com
IdentityFile /c/Users/ThinkPad/.ssh/id_rsa_gitlab
User gitlabuser
配置说明:
- Host :平台别名,可以替代
HostName
,但大部分都习惯用HostName
- HostName :平台域名或者IP地址,如果私服没有域名,只需要填IP地址即可,不需要加端口号,不管端口号是不是自定义;
- IdentityFile :私钥文件的绝对路径,以linux路径风格写,即这样,切记不要写错,很重要
/c/Users/ThinkPad/.ssh/id_rsa_gitlab
- user:即你的账户名
完成以上配置后可以使用ssh -T git@HostName
来测试连通性,第一次会有个提示,提示你是否信任此证书,输入yes
即可,输出以下内容说明添加成功。
$ ssh -T git@github.com
Hi fcmmy! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@gitlab.com
Hi gitlabuser! You've successfully authenticated, but gitlab does not provide shell access.
6.仓库配置
由于取消了git config --global user
的配置,如果需要向仓库提交代码,需要对仓库配置自己的用户信息,否则windows下是提交不了的;
git 的用户配置分为三级别,System > Global >Local。
- System 即系统级别
- Global 为配置的全局
- Local 为仓库级别
优先级是 Local > Global > System
切换到代码仓库所在目录,在gitbash中输入以下内容,配置一次即可,不需要每次都配置:
git config --local user.name "fcmmy"
git config --local user.email "23456@163.com"
git 默认启动ssh-agent配置并默认添加密钥
找到git安装目录下的bash.bashrc
文件,在文件的末尾添加以下内容,路径一定是绝对路径,跟config配置文件中的IdentityFile
一致`,如果有其他的私钥可以一起放到此处;
eval "$(ssh-agent -s)"
ssh-add "/c/Users/ThinkPad/.ssh/id_rsa_github"
ssh-add "/c/Users/ThinkPad/.ssh/id_rsa_gitlab"
参考链接:
https://blog.51cto.com/u_15707676/5443903?abTest=51cto
https://blog.csdn.net/wobofan2006/article/details/126505622