大家好,我是 CC,在这里欢迎大家的到来~
前序
书接上文,如果公司和个人使用不同的代码储存平台,每个项目配置不同的 Git 用户名和邮箱就可以实现分离;但是当公司和个人使用同一个平台时,如何进行区分呢?
操作流程
假设我们现在需要两个 Github 密钥分别用于个人项目和开源项目
查看密钥
查看已有的密钥对
bash
ls -la ~/.ssh
生成密钥
使用两个 Github 账户的注册邮箱分别生成对应的密钥
javascript
ssh-keygen -t ed25519 -C "私人项目邮箱地址" -f ~/.ssh/github_id_ed25519_private
ssh-keygen -t ed25519 -C "开源项目邮箱地址" -f ~/.ssh/github_id_ed25519_public
添加密钥
- 添加到 Github 代码平台 SSH Key 位置
- 添加到 ssh-agent
javascript
ssh-add ~/.ssh/github_id_ed25519_private
ssh-add ~/.ssh/github_id_ed25519_public
SSH 配置
对 ~/.ssh/config
文件进行编辑
bash
Host github_private
HostName github.com
User git
IdentityFile ~/.ssh/github_id_ed25519_private
AddKeysToAgent yes
UseKeychain yes
Host github_public
HostName github.com
User git
IdentityFile ~/.ssh/github_id_ed25519_public
AddKeysToAgent yes
UseKeychain yes
每个配置的作用在上篇文章中有提到
项目配置
克隆远程项目
ruby
# 个人项目
git clone git@github_private:private_account_name/repo_name.git
# 开源项目
git clone git@github_public:public_account_name/repo_name.git
修改本地项目
ruby
git remote set-url origin git@github_public:public_account_name/repo_name.git
新建项目同理
ruby
git remote add origin git@github_public:public_account_name/repo_name.git