管理你的多个 Git 密钥(单平台多账号)

大家好,我是 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

添加密钥

  1. 添加到 Github 代码平台 SSH Key 位置
  2. 添加到 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

参考文章

如何用 SSH 密钥在一台机器上管理多个 GitHub 账户

相关推荐
用户97514707513617 分钟前
在 Vite 中配置 CSS 模块
github
AI成长日志22 分钟前
【实用工具教程】Git进阶:分支策略与合并冲突解决
git
Ne0_bbk31 分钟前
# ComfyUI+OpenClaw协同工作——七个坑,记录
github
wAIxiSeu1 小时前
开源项目分享——CLI-Anything
开源·github
ruanCat1 小时前
加了 .gitattributes 就万事大吉?我差点毁了整个团队的 Git 工作流
git
咋吃都不胖lyh2 小时前
查看 Git 本地仓库关联的远程仓库链接(URL)
git
wheelmouse77882 小时前
AI 时代的 Git 进阶术:如何优雅地让多个 Agent 并行开发
人工智能·git·ai编程
研究点啥好呢2 小时前
3月19日GitHub热门项目推荐|OpenClaw棋逢对手
人工智能·ai·开源·github
猫头虎3 小时前
从零开始,一步步安装和配置OpenClaw汉化版详细安装指南
langchain·开源·prompt·github·aigc·ai编程·agi
wuyaolong0073 小时前
Git误操作急救手册大纲
ui·github