Git configuration of multiple terminals and multiple accounts

First, confirm that Git is installed, and you can use the git –version command to check the currently installed version.

To configure multiple git accounts for the same computer, the overall process is as follows:

1. Clear the default global user.name and user.email

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

Look at the git configuration: git config --global --list

2. Configure multiple git usernames and emails

a. Single configuration

git config --global user.name "yourusername"
git config --global user.email "[email protected]"

b. Multiple configurations

Note: The git config command does not have -global, which means that this is a local setting, that is, this user is from the current project, not global.

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

c. Delete the configuration

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

3. Generate multiple keys

The administrator opens the console

a. Generate SSH for the gitte repository

Specify the file path to facilitate subsequent operations: ~/.ssh/id_rsa.gitte, id_rsa.github is the alias of the key.

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

b. Generate SSH for github repository

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

4. Add ssh-key to the ssh-agent trust list separately

$ssh-agent bash
$ssh-add ~/.ssh/id_rsa.gitte
$ssh-add ~/.ssh/id_rsa.github

If you see Identitiy added: ~/.ssh/id_ras_github, it means that the addition was successful.

5. Add the public key to your git account

Use the command, copy the public key, and paste it into your git account. Or open the file copy, with pub

pbcopy < ~/.ssh/id_rsa.gitte

Add step reference: https://www.jianshu.com/p/68578d52470c

6. Configure multiple ssh-keys in the config file

#Default gitHub user Self
Host github.com
    HostName github.com
    User git #默认就是git,可以不写
    IdentityFile ~/.ssh/id_rsa.github
	
# gitee的配置
host gitee.com  # 别名,最好别改
	Hostname gitee.com #要连接的服务器
	User [email protected] #用户名
	#密钥文件的地址,注意是私钥
	IdentityFile ~/.ssh/id_rsa_gitte

#Add gitLab user 
Host git.lingban.cn
    HostName git.lingban.cn
    User [email protected]
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa_lingban

7. Testing

$ssh -T [email protected]

8. Configure SSH matching for multiple accounts


In the .ssh directory, create a new config file and configure the multi-user key:

host user_a_github.com
Hostname github.com
User git
IdentityFile ~/.ssh/user_a_id_rsa

host user_b_github.com
Hostname github.com
User git
IdentityFile ~/.ssh/user_b_id_rsa

8. Some orders

检查当前用户
$ssh -vT [email protected]
检查当初密钥
$ssh-add -l
添加密钥
ssh-add ~/.ssh/id_rsa.xxx
删除密钥
$ssh-add -d /Users/****/.ssh/id_rsa
查看 git config 配置
$git config --list

Scroll to Top