跳至主要內容

Mac 中配置多个 Git 账号

Bing🐣GitGitGithubGitlabGitee大约 2 分钟

Mac 中配置多个 Git 账号

记录下日常开发中需要同时配置 Github、Gitlab、Gitee 多 Git 账号情况。

清空全局用户配置

如果之前使用过--global设置过全局的用户名和邮箱,需要使用下面的命令清空掉。

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

生成密钥

使用下面的命令生成所需要的各平台密钥。

ssh-keygen -t rsa -C 'Github邮箱' -f ~/.ssh/id_rsa_github
ssh-keygen -t rsa -C 'Gitlab邮箱' -f ~/.ssh/id_rsa_gitlab
ssh-keygen -t rsa -C 'Gitee邮箱' -f ~/.ssh/id_rsa_gitee

进入到.ssh文件中就能看到新生成的这几个密钥文件了。

image
image

配置密钥

Github

Github 可在SSH and GPG keysopen in new window中点击New SSH key按钮来添加 SSH key。

image
image

填写一个容易记住的 Title,复制密钥内容填入 Key,密钥内容可通过cat id_rsa_github.pub查看pub文件中的密钥,密钥内容以ssh-rsa开头,Github邮箱为结尾。

image
image
image
image

Gitlab

同理 Github,可在 User Settings - SSH Keysopen in new window中配置

image
image

Gitee

同理 Github,可在设置-SSH 公钥open in new window中配置。
image

配置 Config

.ssh中新建一个config文件。

cd ~/.ssh
touch config
vim config

通过vim编辑该文件,填入以下配置内容。

Host github.com
HostName github.com
User github邮箱
IdentityFile ~/.ssh/id_rsa_github

Host gitlab.com
HostName gitlab.com
User gitlab邮箱
IdentityFile ~/.ssh/id_rsa_gitlab

Host gitee.com
HostName gitee.com
User gitee邮箱
IdentityFile ~/.ssh/id_rsa_gitee

测试连通性

ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T git@gitee.com

以 Github 为例,首次输入ssh -T git@github.com后会提示Are you sure you want to continue connecting``,输入yes后如果看到successfully字样说明就成功了,如果出现Permission denied说明失败,需要检查密钥是否配置以及是否复制有误。

image
image