SSH多用户配置

多用户配置

参考链接

首先找到SSH文件目录

1
C:\Users\test\.ssh

目录里应该有config文件, 保证格式如下

1
2
3
4
5
6
7
8
9
10
11
12
13
# Github user1 (default user)
Host github.com
HostName github.com
PreferredAuthentications publickey
# 指定特定的ssh私钥文件
IdentityFile ~/.ssh/id_rsa

# Github user2
Host github-enterhalf
HostName github.com
PreferredAuthentications publickey
# 指定特定的ssh私钥文件
IdentityFile ~/.ssh/id_rsa_enterhalf

Host需要保证是唯一的, 连接ssh时, 选择不同的Host名进行连接

原本的ssh:

1
git@github.com:enterhalf/enterhalf.github.io.git

更改后的ssh, 与上方定义的相同即可:

1
git@github-enterhalf:enterhalf/enterhalf.github.io.git

原本测试ssh是否正常连接到Github:

1
ssh -T git@github.com

更改后测试ssh是否正常连接到Github:

1
ssh -T git@github-enterhalf

HostName 则是各个代码托管平台的地址: github.com gitee.com

自定义提交用户信息

参考链接

SSH秘钥设置成功后, 无需登录Git账号即可推送到Github仓库
由于无需登录, 在Github仓库上显示的提交用户可以是任何人

提交用户的显示优先级分为三级别, System —> Global —>Local; System 即系统级别, 为系统主机名, Global 为配置的全局, Local 为仓库级别, 优先级是 Local > Global > System

进入仓库文件夹后, 输入以下命令, 即可更改仓库的Local信息

更改提交人昵称

1
git config --local user.name "enterhalf"

更改提交人邮箱信息, Github会自动关联该邮箱

1
git config --local user.email "enterhalf@qq.com"

清除global用户信息

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

总结

  • 下次要使用第二个用户的时候, 仓库的ssh需要自己修改一次
  • 使用第二个用户时, 仓库的local提交用户也需要更改

参考链接

Github配置SSH官方文档


SSH多用户配置
https://enterhalf.github.io/2022/08/23/SSH多用户配置/
作者
enterhalf
发布于
2022年8月23日
许可协议