在工作中,我们可能需要将代码推送到公司内部的自建 Git 代码托管平台(如 GithLab)。与此同时,日常开发过程中,我们也常常需要将代码提交到社区的 Git 代码托管平台(如 Github 等)。提交到 github 又需要管理多个 github 账号
解决
用过~/.ssh/config
配置多个不同的 SSH 认证
# 公司github
ssh-keygen -t rsa -C "YourCompanyEmailAdress"
# 输入密钥的名称: id_rsa_company
Enter file in which to save the key (/Users/username/.ssh/id_rsa): id_rsa_company
# github1
ssh-keygen -t rsa -C "GithubEmail1"
# 输入密钥的名称: id_rsa_github
Enter file in which to save the key (/Users/username/.ssh/id_rsa): id_rsa_github_1
# github12
ssh-keygen -t rsa -C "GithubEmail2"
# 输入密钥的名称: id_rsa_github
Enter file in which to save the key (/Users/username/.ssh/id_rsa): id_rsa_github_2
代码语言:javascript复制# `~/.ssh/config
# 小号github
Host xx.github.com # 小号
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github_2
# 默认github
Host github
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github_1
Host company-git
User git
# 替换为公司的 Git 代码托管平台的服务器
Hostname company-git-repo.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_company
大号不变,小号 clone 使用 host匹配
git clone git@xx.github.com:xxxx/xxx.git
添加
代码语言:javascript复制ssh-add ~/.ssh/id_rsa_github_1
仓库级别配置 git 提交信息
代码语言:javascript复制git config user.name xxx && git config user.email xxxx@qq.com
git配置``
代码语言:javascript复制# 全局
cat ~/.gitconfig
# 仓库
cat .git/config