在开发过程中,经常会遇到git多用户情况,如一个github用户,一个企业内部gitlab用户。本文简要介绍Window下多用户配置。
在多用户情况下,尽量不要设置全局用户信息。
#添加全局用户信息
git config --global user.name "用户名"
git config --global user.email "邮箱"
#删除全局用户信息
git config --global --unset user.name
git config --global --unset user.email
1、安装并完成git配置后,打开git bash命令行,切换到ssh目录。
cd ~/.ssh
2、生成的多个SSH Key
使用ssk-keygen生成github用户ssh key,密钥文件is_rsa_github,公钥文件is_rsa_github.pub
$ ssh-keygen -t rsa -C "github@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_github
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_github.
Your public key has been saved in id_rsa_github.pub.
The key fingerprint is:
SHA256:K8ZzHA4rrhgHlv7qyP+dAmvpQIq+jPUpbMdjXZncAnE github@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| |
| . E |
| o |
| . . |
|.+ .oS+ |
|*. . . =*o. |
|++o.=.*.=. |
|*=**==o+. |
|=OBO=o.o |
+----[SHA256]-----+
使用ssk-keygen生成gitlab用户ssh key,密钥文件is_rsa_gitlab,公钥文件is_rsa_gitlab.pub
$ ssh-keygen -t rsa -C "gitlab@qq.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_gitlab
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa_gitlab.
Your public key has been saved in id_rsa_gitlab.pub.
The key fingerprint is:
SHA256:2q7mo4MTScmOvsjl9Gk++rUwqNXvUpW7OeQdGdC95Mo gitlab@qq.com
The key's randomart image is:
+---[RSA 2048]----+
| . . |
| . . o |
| . . o o . |
| + o . o |
| + . S o + |
|. + o + o E |
|. B +o.+ + . |
|o.O o+O..= . |
|.+.+*X**. . |
+----[SHA256]-----+
生成文件如下:
$ ls
config id_rsa_github.pub id_rsa_gitlab.pub
id_rsa_github id_rsa_gitlab known_hosts
3、拷贝公钥账户到github个gitlab的SSH keys中
将id_rsa_github.pub中的公钥拷贝到github个人账户的SSH keys中:
将id_rsa_gitlab.pub中的公钥拷贝到gitlab个人账户的SSH keys中:
4、 在.ssh目录创建config文本文件并完成相关配置
#该文件用于配置私钥对应的服务器
Host github.com
User github
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
Host 192.168.12.5
User gitlab
Hostname 192.168.12.5
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitlab
5、将密钥加入密钥管理器
$ ssh-add ~/.ssh/id_rsa_github
Enter passphrase for /c/Users/Administrator/.ssh/id_rsa_github:
Identity added: /c/Users/Administrator/.ssh/id_rsa_github (/c/Users/Administrator/.ssh/id_rsa_github)
$ ssh-add ~/.ssh/id_rsa_gitlab
Enter passphrase for /c/Users/Administrator/.ssh/id_rsa_gitlab:
Identity added: /c/Users/Administrator/.ssh/id_rsa_gitlab (/c/Users/Administrator/.ssh/id_rsa_gitlab)
如遇到Could not open a connection to your authentication agent.提示,输入一下命令,在进行ssh-add
$ ssh-agent bash
6、测试
$ ssh -T git@github.com
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa_github':
Hi github! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@192.168.12.5
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa_gitlab':
Welcome to GitLab, @gitlab!
测试通过后,就可以在电脑上同时使用git多账号同时操作,互不影响。