同时在github和gitee配置密钥

同时在github和gitee配置密钥

1. 生成不同的 SSH 密钥

为每个平台生成单独的 SSH 密钥。

bash 复制代码
# 为 GitHub 生成密钥(默认文件路径为 ~/.ssh/github_id_rsa)
ssh-keygen -t rsa -b 4096 -C "your_github_email@example.com" -f ~/.ssh/github_id_rsa

# 为 Gitee 生成密钥(默认文件路径为 ~/.ssh/gitee_id_rsa)
ssh-keygen -t rsa -b 4096 -C "your_gitee_email@example.com" -f ~/.ssh/gitee_id_rsa

执行命令后,根据提示按回车确认,设置密钥存储路径。可以为密钥设置密码,也可以直接回车跳过。


2. 添加 SSH 密钥到 SSH Agent(可选做这一步,一般不用)

将生成的密钥添加到 SSH Agent 以便使用。

bash 复制代码
# 启动 SSH Agent
eval "$(ssh-agent -s)"

# 添加 GitHub 的密钥
ssh-add ~/.ssh/github_id_rsa

# 添加 Gitee 的密钥
ssh-add ~/.ssh/gitee_id_rsa

3. 配置密钥到对应的平台

GitHub
  1. 登录 GitHub SSH 设置页面

  2. 打开生成的 GitHub 公钥文件:

    bash 复制代码
    cat ~/.ssh/github_id_rsa.pub
  3. 将公钥内容复制到 GitHub 的新 SSH 密钥中,保存。

Gitee
  1. 登录 Gitee SSH 设置页面

  2. 打开生成的 Gitee 公钥文件:

    bash 复制代码
    cat ~/.ssh/gitee_id_rsa.pub
  3. 将公钥内容复制到 Gitee 的新 SSH 密钥中,保存。


4. 配置 SSH Config 文件

为每个平台配置不同的 SSH 地址。

编辑或创建 ~/.ssh/config 文件,添加以下内容:

plaintext 复制代码
# GitHub 配置
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github_id_rsa

# Gitee 配置
Host gitee.com
    HostName gitee.com
    User git
    IdentityFile ~/.ssh/gitee_id_rsa

保存并关闭文件。


5. 测试连接

测试 GitHub
bash 复制代码
ssh -T git@github.com

如果配置正确,会提示:

复制代码
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
测试 Gitee
bash 复制代码
ssh -T git@gitee.com

如果配置正确,会提示:

复制代码
Welcome to Gitee.com, <username>!

6. 使用多个平台的远程仓库

克隆仓库时(直接复制平台的ssh链接即可):
  • 对于 GitHub:

    bash 复制代码
    git clone git@github.com:username/repo.git
  • 对于 Gitee:

    bash 复制代码
    git clone git@gitee.com:username/repo.git
推送或拉取:

Git 会根据 ~/.ssh/config 中的配置自动匹配正确的密钥。


enjoy!

相关推荐
郭钊荣13 分钟前
为什么 OpenClaw 能出圈:扒一扒小龙虾的agent系统设计
后端·github
Moment1 小时前
Cursor 的 5 种指令方法比较,你最喜欢哪一种?
前端·后端·github
子兮曰12 小时前
async/await高级模式:async迭代器、错误边界与并发控制
前端·javascript·github
崔庆才丨静觅1 天前
Claude Code GitHub Actions 使用教程
github·api·claude
砖厂小工2 天前
用 GLM + OpenClaw 打造你的 AI PR Review Agent — 让龙虾帮你审代码
android·github
程序员鱼皮2 天前
又一个新项目完结,我要出海了!
ai·github·开源项目
徐小夕2 天前
pxcharts-vue:一款专为 Vue3 打造的开源多维表格解决方案
前端·vue.js·github
Moment2 天前
想要长期陪伴你的助理?先从部署一个 OpenClaw 开始 😍😍😍
前端·后端·github