# 一个设备配置多个git key URL: https://chengjinfang.com/posts/2018/manual-git2/ Author: 程锦方 Date: 2018-04-11 Tags: #manual #note Summary: 在一台设备上分别配置多个 Git 身份,包括 SSH 密钥、主机别名和仓库用户信息。 ------------------------------------------------------------------------ 全文摘自https://www.jianshu.com/p/93f846cef40d ### 生成key ```sh ssh-keygen -t rsa -C "mail" ``` ### 在.ssh文件夹添加config ```text 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 缓存 ```sh ssh-add -D ssh-add ~/.ssh/id_rsa_1 ssh-add ~/.ssh/id_rsa_2 ``` ### 设置git用户名和邮箱 ```sh # 取消全局 用户名/邮箱 配置 $ git config --global --unset user.name $ git config --global --unset user.email # 进入项目文件夹,单独设置每个repo 用户名/邮箱 $ git config user.email "mail" $ git config user.name "name" ``` ### 重建 origin ```sh git remote rm origin git remote add origin git@github.com:user/xxx.git ``` ssh 客户端是通过类似 git@github.com:user/xxx.git 的地址来识别使用本地的哪个私钥的,地址中的 User 是@前面的git, Host 是@后面的github.com。 ------------------------------------------------------------------------ Site: https://chengjinfang.com/ · All Index: https://chengjinfang.com/all/ · RSS: https://chengjinfang.com/rss.xml