Git 多账号配置
2024-03-28 10:32:53 # Technical # Notes

一个 Git 客户端上同时配置多个远程仓库账号,如:GitHub、Gitee、GitLab

全局配置

若不是第一次配置,需清除配置

1
2
3
4
5
6
# 查看所有配置
git config --global -l

# 删除全局配置
git config --global --unset user.name
git config --global --edit

配置全局用户名和邮箱

1
2
git config --global user.name venom
git config --global user.email [email protected]

创建 SSH KEYS

进入 C:\Users\<用户>\.ssh 没有 .ssh 文件夹,可新建

gitbash here

1
2
3
4
5
# 创建 GitHub ssh keys
ssh-keygen -t rsa -f ~/.ssh/id_rsa.github -C "github email"

# 创建 Gitee ssh keys
ssh-keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "gitee email"

不需设置密码,回车即可

识别新密钥

将新生成的密钥加入到 SSH agent 中

1
2
3
ssh-agent bash
ssh-add ~/.ssh/id_rsa.github
ssh-add ~/.ssh/id_rsa.gitee

配置 config

编辑 .ssh 下的 config 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
#Default github user Self
Host github.com
HostName github.com
#Default is git, can ignore
User git
IdentityFile ~/.ssh/id_rsa_github

#Add gitee user
Host gitee.com
Port 22
HostName gitee.com
User git
IdentityFile ~/.ssh/id_rsa_gitee

远程仓库添加 SSH

到远程仓库中配置本地生成的公钥 id_rsa_***.pub

测试

1
2
ssh -T [email protected]
ssh -T [email protected]