锦方的个人网页

一个设备配置多个git key

发布 · 约 89 字 · 预计 1 分钟

在一台设备上分别配置多个 Git 身份,包括 SSH 密钥、主机别名和仓库用户信息。

归档记录:本文最后更新于 2018 年,文中涉及的软件版本、配置或命令可能已随环境演进发生变化。

本文目录(共 5 节)

全文摘自https://www.jianshu.com/p/93f846cef40d

生成key #

ssh-keygen -t rsa -C "mail"

在.ssh文件夹添加config #

Host github.com  #不同点,可更改
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_1

Host github2.com   #不同点
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_2

清空本地的 SSH 缓存 #

ssh-add -D
ssh-add ~/.ssh/id_rsa_1
ssh-add ~/.ssh/id_rsa_2

设置git用户名和邮箱 #

# 取消全局 用户名/邮箱 配置
$ git config --global --unset user.name
$ git config --global --unset user.email
# 进入项目文件夹,单独设置每个repo 用户名/邮箱
$ git config user.email "mail"
$ git config user.name "name"

重建 origin #

git remote rm origin
git remote add origin [email protected]:user/xxx.git

ssh 客户端是通过类似 [email protected]:user/xxx.git 的地址来识别使用本地的哪个私钥的,地址中的 User 是@前面的git, Host 是@后面的github.com。

✉ 对本文有补充或勘误?欢迎就本文写信交流。