Git configure multiple accounts in Windows

(1) generate and deploy SSH key

After installing Git client, open the git bash, enter the following command to generate user1's SSH Key:

 ssh-keygen -t rsa -C "[email protected]" 

.Ssh directory in the current user's private key file and will generate id_rsa id_rsa.pub public key file, add the contents of id_rsa.pub to user1 in the github. Then enter the following command in the git bash test the user's SSH key is to take effect:

ssh -T [email protected] 

If the connection is successful tips ! Hi user1 You've successfully authenticated, but GitHub does not provide shell access.
Note: This command is limited to a file named id_rsa the key.

Then generate a key user2, pay attention to no longer use the default file name id_rsa, otherwise it will cover before key file:

ssh-keygen -t rsa -f ~/.ssh/id_rsa2 -C "[email protected]" 

Then add the file to the user's public key in github.
Test user2's ssh key files need to specify connection:

ssh -T [email protected] -i ~/.ssh/id_rsa2 

You can also use ssh agent after adding key test. Because the system default read only id_rsa, in order to allow ssh recognize the new private key, the private key can be added using ssh-agent manually:  

ssh-agent bash
ssh-add ~/.ssh/id_rsa2

 Note: This method only effective the current window, open a new window of the ssh connection fails.

 

(2) Configuration config file

Create a text config file in the .ssh directory, configure each account a Host node. The main Item Description: 

?
1
2
3
4
5
Host      主机别名
HostName  服务器真实地址
IdentityFile  私钥文件路径
PreferredAuthentications  认证方式
User  用户名

Profile content

Copy the code
# 配置user1 
Host u1.github.com
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa
PreferredAuthentications publickey
User user1

# 配置user2
Host u2.github.com
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa2
PreferredAuthentications publickey
User user2
Copy the code

And through the terminal SSH Key test whether the entry into force

 

(3) configure the user name and mailbox

If you had previously configured global user name and mailbox, you need to cancel the configuration, and then configure the appropriate user name and mailbox in each warehouse.

git config --global --unset user.name
git config --global --unset user.email

Configuring user name and mailbox for each individual warehouse

git config user.name "user1"
git config user.email "[email protected]"

如果原先使用HTTPS通信,则需要修改远程仓库地址

git remote rm origin
git remote add origin [email protected]:xxx/xxxxx.git


注意:

ssh -T git@github 时,一定要打 yes!yes!yes!!! 我就是被这里坑了!

 

用法:

 

安装好Git客户端后,打开git bash,输入以下命令生成user1的SSH Key:

 ssh-keygen -t rsa -C "[email protected]" 

在当前用户的.ssh目录下会生成id_rsa私钥文件和id_rsa.pub公钥文件,将id_rsa.pub中的内容添加至user1的github中。然后在git bash中输入以下命令测试该用户的SSH密钥是否生效:

ssh -T [email protected] 

若连接成功则提示Hi user1! You've successfully authenticated, but GitHub does not provide shell access.
注:该命令仅限于文件名为id_rsa的密钥。

接着生成user2的密钥,注意不能再使用默认的文件名id_rsa,否则会覆盖之前密钥文件:

ssh-keygen -t rsa -f ~/.ssh/id_rsa2 -C "[email protected]" 

再将该用户的公钥文件添加至github中。
测试user2的ssh连接时需要指定密钥文件:

ssh -T [email protected] -i ~/.ssh/id_rsa2 

也可以使用ssh agent添加密钥后进行测试。因为系统默认只读取id_rsa,为了让ssh识别新的私钥,可以使用ssh-agent手动添加私钥:  

ssh-agent bash
ssh-add ~/.ssh/id_rsa2

 注:该方法仅限当前窗口有效,打开新的窗口则ssh连接失败。

 

(2)配置config文件

在.ssh目录下创建一个config文本文件,每个账号配置一个Host节点。主要配置项说明: 

?
1
2
3
4
5
Host      主机别名
HostName  服务器真实地址
IdentityFile  私钥文件路径
PreferredAuthentications  认证方式
User  用户名

配置文件内容

Copy the code
# 配置user1 
Host u1.github.com
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa
PreferredAuthentications publickey
User user1

# 配置user2
Host u2.github.com
HostName github.com
IdentityFile C:\\Users\\Administrator\\.ssh\\id_rsa2
PreferredAuthentications publickey
User user2
Copy the code

再通过终端测试SSH Key是否生效

 

(3)配置用户名和邮箱

如果之前配置过全局的用户名和邮箱,需要取消相关配置,再在各仓库下配置相应的用户名和邮箱。

git config --global --unset user.name
git config --global --unset user.email

为各仓库单独配置用户名和邮箱

git config user.name "user1"
git config user.email "[email protected]"

If the original use of HTTPS communications, you need to modify a remote repository address

git remote rm origin
git remote add origin [email protected]:xxx/xxxxx.git

Guess you like

Origin www.cnblogs.com/feiquan/p/11538433.html