同时在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!

相关推荐
洛宇12 小时前
一个口语 skill,灵感居然来自2021年的那个夏天
人工智能·程序员·github
LienJack14 小时前
我做了一个 AI Agent 学习站
github·agent
一点一木15 小时前
🚀 2026 年 5 月 GitHub 十大热门项目排行榜 🔥
人工智能·github·ai编程
逛逛GitHub16 小时前
GitHub 狂揽 1.3 万 Star,Anthropic 开源的知识工作者插件。
github
程序员鱼皮18 小时前
我用 GitHub 仓库养 AI 龙虾,自动开发上线项目!保姆级教程
前端·人工智能·ai·程序员·github·编程·ai编程
NPE~20 小时前
[手写系列]从零到一:Github开源你的第一个项目
ai·开源·github·教程·项目实战·规范·yiqguard
似夜晓星辰20 小时前
Markdown文本编辑器Typora平替
编辑器·github
右耳朵猫AI21 小时前
Github趋势榜 2026年第20周
github
Pluchon21 小时前
萌萌技术分享笔记——Java综合项目
java·开发语言·笔记·git·github·mybatis·postman
散峰而望1 天前
【算法练习】算法练习精选:从 Phone numbers 到 Decrease,覆盖字符串、模拟、图论思维题
数据结构·c++·算法·贪心算法·github·动态规划·图论