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

相关推荐
u13013011 小时前
GitHub 热榜项目:日榜(2026-10-27)
人工智能·github
zroooo11 小时前
GitHub Flow
github
粥里有勺糖12 小时前
视野修炼-技术周刊第137期 | 编程已经面目全非
前端·github·ai编程
miofly12 小时前
GitHub 今日推荐|rea:用 MCP 把反编译器和 AI agent 接到一起的逆向工具
typescript·开源·github
MicrosoftReactor13 小时前
技术速递|超越一刀切:为 GitHub Copilot CLI 和 VS Code 实现决策路由
vscode·ai·github·copilot·决策路由
m4Rk_13 小时前
【论文阅读】Agent 记忆机制(113):Agentic Memory Management——从“检索相关信息”走向“管理 Agent 内部记忆”
论文阅读·人工智能·学习·开源·github
技术达芬奇15 小时前
git bundle介绍
github
暖心hebbie设计学习15 小时前
GitHub 热榜拆解(10.28)|5 个 AI 工具:从设计到写代码,附 issue 区坑点
人工智能·github
逛逛GitHub15 小时前
看这 11 个案例,AI 做游戏真的越来越顶了。
github
前端小万16 小时前
想做个人副业别乱买服务器!我的 5 个月真实开销分享
前端·后端·github