在開發過程中,經常會遇到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 "[email protected]"
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 [email protected]
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 "[email protected]"
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 [email protected]
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 [email protected]
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 [email protected]
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa_gitlab':
Welcome to GitLab, @gitlab!
測試通過後,就可以在電腦上同時使用git多賬號同時操作,互不影響。