Skip to content

git 同时配置 github gitee 多个密钥

Terminal window
# GitHub
ssh-keygen -t ed25519 -C "i@lihaha.cn" -f ~/.ssh/github.ssh
# 码云
ssh-keygen -t ed25519 -C "i@lihaha.cn" -f ~/.ssh/gitee.ssh
Terminal window
# 比如 GitHub
https://github.com/settings/ssh/new
Terminal window
# 1. 创建一个 config 文件
~\.ssh\config
# 2. 添加配置
# gitee
Host gitee
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee.ssh
# github
Host github
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github.ssh
Terminal window
# 当出现用户名的时候代表成功
# GitHub
ssh -T git@github.com
Terminal window
# 设置全局用户
git config --global user.name "lihaha"
git config --global user.email "i@lihaha.cn"
# 设置单个仓库用户
git config --local user.name "lihaha2"
git config --local user.email "lihaha2@lihaha.cn"
# 根据目录自动配置用户
# file ~/.gitconfig
[user]
name = lihaha
email = i@lihaha.cn
[includeIf "gitdir:~/Projects/"]
path = ~/.gitconfig-projects
[includeIf "gitdir:/Users/{name}/Project/"]
path = ~/.gitconfig-projects
# file ~/.gitconfig-projects
[user]
name = lihaha2
email = lihaha2@lihaha.cn
# 其他推荐的配置
# file ~/.gitconfig
[push]
autoSetupRemote = true
[log]
date = format:%Y-%m-%d %H:%M:%S %A
# 查看配置
git config --list