管理你的多个 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 账户

相关推荐
海绵不是宝宝8171 小时前
连接远程服务器上的 jupyter notebook,解放本地电脑
服务器·jupyter·github
黑客飓风4 小时前
当GitHub宕机时,我们如何协作?
github·notepad++
xiAo_Ju6 小时前
git hooks配置
git
mit6.8248 小时前
[Git] 如何拉取 GitHub 仓库的特定子目录
git·github
用户466537015058 小时前
如何在 IntelliJ IDEA 中可视化压缩提交到生产分支
后端·github
用户466537015058 小时前
git代码压缩合并
后端·github
极小狐9 小时前
GitLab 安全漏洞 CVE-2025-7739 解决方案
ci/cd·gitlab·devsecops·devops·极狐gitlab
若水晴空初如梦9 小时前
QT聊天项目DAY19
github
许心月9 小时前
Git#revert
git
张较瘦_12 小时前
[论文阅读] 人工智能 | 当Hugging Face遇上GitHub:预训练语言模型的跨平台同步难题与解决方案
论文阅读·人工智能·github