Git 同时管理 Gitee 和 GitHub 的 SSH 配置实战

1. 引言

在日常开发中,很多开发者会同时使用 Gitee(码云)和 GitHub 两个代码托管平台:GitHub 面向全球开源社区,Gitee 在国内访问速度快、私有仓库免费。但默认情况下,Git 的 SSH 密钥是「一人一钥」,同一台电脑上如何让两个平台互不干扰、各自使用独立的密钥完成推送和拉取?本文就带你一步步配置好「一套电脑、两把钥匙、两个平台」的 SSH 多账号方案。

2. 原理说明

2.1 SSH 如何区分不同平台

Git 通过 SSH 协议连接远程仓库时,默认读取 ~/.ssh/config 配置文件。该文件可以针对不同的 Host(主机别名)指定不同的:

  • HostName:真实域名(如 github.comgitee.com
  • User:登录用户(Git 平台固定为 git
  • IdentityFile:使用的私钥路径

因此,只要为 Gitee 和 GitHub 分别生成一对密钥,并在 config 中做好映射,Git 就能自动选择正确的密钥完成认证。

2.2 整体流程

#mermaid-svg-GlqG8a7ujtTWBj9g{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-GlqG8a7ujtTWBj9g .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-GlqG8a7ujtTWBj9g .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-GlqG8a7ujtTWBj9g .error-icon{fill:#552222;}#mermaid-svg-GlqG8a7ujtTWBj9g .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-GlqG8a7ujtTWBj9g .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-GlqG8a7ujtTWBj9g .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-GlqG8a7ujtTWBj9g .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-GlqG8a7ujtTWBj9g .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-GlqG8a7ujtTWBj9g .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-GlqG8a7ujtTWBj9g .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-GlqG8a7ujtTWBj9g .marker{fill:#333333;stroke:#333333;}#mermaid-svg-GlqG8a7ujtTWBj9g .marker.cross{stroke:#333333;}#mermaid-svg-GlqG8a7ujtTWBj9g svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-GlqG8a7ujtTWBj9g p{margin:0;}#mermaid-svg-GlqG8a7ujtTWBj9g .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-GlqG8a7ujtTWBj9g .cluster-label text{fill:#333;}#mermaid-svg-GlqG8a7ujtTWBj9g .cluster-label span{color:#333;}#mermaid-svg-GlqG8a7ujtTWBj9g .cluster-label span p{background-color:transparent;}#mermaid-svg-GlqG8a7ujtTWBj9g .label text,#mermaid-svg-GlqG8a7ujtTWBj9g span{fill:#333;color:#333;}#mermaid-svg-GlqG8a7ujtTWBj9g .node rect,#mermaid-svg-GlqG8a7ujtTWBj9g .node circle,#mermaid-svg-GlqG8a7ujtTWBj9g .node ellipse,#mermaid-svg-GlqG8a7ujtTWBj9g .node polygon,#mermaid-svg-GlqG8a7ujtTWBj9g .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-GlqG8a7ujtTWBj9g .rough-node .label text,#mermaid-svg-GlqG8a7ujtTWBj9g .node .label text,#mermaid-svg-GlqG8a7ujtTWBj9g .image-shape .label,#mermaid-svg-GlqG8a7ujtTWBj9g .icon-shape .label{text-anchor:middle;}#mermaid-svg-GlqG8a7ujtTWBj9g .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-GlqG8a7ujtTWBj9g .rough-node .label,#mermaid-svg-GlqG8a7ujtTWBj9g .node .label,#mermaid-svg-GlqG8a7ujtTWBj9g .image-shape .label,#mermaid-svg-GlqG8a7ujtTWBj9g .icon-shape .label{text-align:center;}#mermaid-svg-GlqG8a7ujtTWBj9g .node.clickable{cursor:pointer;}#mermaid-svg-GlqG8a7ujtTWBj9g .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-GlqG8a7ujtTWBj9g .arrowheadPath{fill:#333333;}#mermaid-svg-GlqG8a7ujtTWBj9g .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-GlqG8a7ujtTWBj9g .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-GlqG8a7ujtTWBj9g .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-GlqG8a7ujtTWBj9g .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-GlqG8a7ujtTWBj9g .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-GlqG8a7ujtTWBj9g .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-GlqG8a7ujtTWBj9g .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-GlqG8a7ujtTWBj9g .cluster text{fill:#333;}#mermaid-svg-GlqG8a7ujtTWBj9g .cluster span{color:#333;}#mermaid-svg-GlqG8a7ujtTWBj9g div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-GlqG8a7ujtTWBj9g .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-GlqG8a7ujtTWBj9g rect.text{fill:none;stroke-width:0;}#mermaid-svg-GlqG8a7ujtTWBj9g .icon-shape,#mermaid-svg-GlqG8a7ujtTWBj9g .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-GlqG8a7ujtTWBj9g .icon-shape p,#mermaid-svg-GlqG8a7ujtTWBj9g .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-GlqG8a7ujtTWBj9g .icon-shape .label rect,#mermaid-svg-GlqG8a7ujtTWBj9g .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-GlqG8a7ujtTWBj9g .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-GlqG8a7ujtTWBj9g .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-GlqG8a7ujtTWBj9g :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 生成 GitHub 密钥
生成 Gitee 密钥
配置 ~/.ssh/config
分别添加公钥到平台
测试连接
修改仓库远程地址
日常推送拉取

3. 准备工作

开始之前,请确认本机已安装 Git 并配置好基础信息:

bash 复制代码
git --version
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

提示:user.nameuser.email 是提交记录的作者信息,与 SSH 密钥无关,但建议先配置好。

4. 生成两把 SSH 密钥

4.1 生成 GitHub 专用密钥

bash 复制代码
ssh-keygen -t ed25519 -C "github-你的邮箱" -f ~/.ssh/id_ed25519_github

执行后按提示设置 passphrase(可直接回车跳过)。生成的文件为:

  • ~/.ssh/id_ed25519_github(私钥)
  • ~/.ssh/id_ed25519_github.pub(公钥)

4.2 生成 Gitee 专用密钥

bash 复制代码
ssh-keygen -t ed25519 -C "gitee-你的邮箱" -f ~/.ssh/id_ed25519_gitee

生成的文件为:

  • ~/.ssh/id_ed25519_gitee(私钥)
  • ~/.ssh/id_ed25519_gitee.pub(公钥)

4.3 查看公钥内容

bash 复制代码
cat ~/.ssh/id_ed25519_github.pub
cat ~/.ssh/id_ed25519_gitee.pub

复制输出的内容,下一步需要分别粘贴到两个平台。

5. 配置 SSH config 文件

5.1 创建/编辑 config

bash 复制代码
vim ~/.ssh/config

5.2 写入配置

text 复制代码
# GitHub
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github
  IdentitiesOnly yes

# Gitee
Host gitee.com
  HostName gitee.com
  User git
  IdentityFile ~/.ssh/id_ed25519_gitee
  IdentitiesOnly yes

说明:IdentitiesOnly yes 表示只使用指定的密钥,避免 Git 尝试所有密钥导致认证失败。

5.3 设置文件权限

bash 复制代码
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_ed25519_github
chmod 600 ~/.ssh/id_ed25519_gitee

6. 在平台添加公钥

6.1 GitHub 添加公钥

  1. 登录 GitHub,进入 Settings → SSH and GPG keys
  2. 点击 New SSH key
  3. Title 随意填写(如 My MacBook),Key 粘贴 id_ed25519_github.pub 的内容;
  4. 点击 Add SSH key 保存。

6.2 Gitee 添加公钥

  1. 登录 Gitee,进入 设置 → SSH 公钥
  2. 标题随意填写,公钥粘贴 id_ed25519_gitee.pub 的内容;
  3. 点击 确定 保存。

7. 测试连接

7.1 测试 GitHub

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

看到以下输出即表示成功:

text 复制代码
Hi 你的用户名! You've successfully authenticated, but GitHub does not provide shell access.

7.2 测试 Gitee

bash 复制代码
ssh -T git@gitee.com

看到以下输出即表示成功:

text 复制代码
Hi 你的用户名! You've successfully authenticated, but Gitee does not provide shell access.

8. 修改仓库远程地址

8.1 新仓库直接使用 SSH 地址

克隆时直接使用 SSH 地址即可:

bash 复制代码
git clone git@github.com:用户名/仓库名.git
git clone git@gitee.com:用户名/仓库名.git

8.2 已有仓库修改远程地址

查看当前远程地址:

bash 复制代码
git remote -v

修改为 SSH 地址:

bash 复制代码
git remote set-url origin git@github.com:用户名/仓库名.git

或:

bash 复制代码
git remote set-url origin git@gitee.com:用户名/仓库名.git

8.3 同时关联两个平台(可选)

如果希望一个仓库同时推送到 Gitee 和 GitHub,可以添加多个 remote:

bash 复制代码
git remote add gitee git@gitee.com:用户名/仓库名.git
git remote add github git@github.com:用户名/仓库名.git

推送时分别执行:

bash 复制代码
git push gitee main
git push github main

9. 常见问题排查

9.1 连接超时或认证失败

  • 确认公钥已正确粘贴到对应平台;
  • 确认 ~/.ssh/configIdentityFile 路径正确;
  • 执行 ssh -vT git@github.com 查看详细日志。

9.2 提示权限太开放

bash 复制代码
chmod 600 ~/.ssh/id_ed25519_github
chmod 600 ~/.ssh/id_ed25519_gitee
chmod 700 ~/.ssh

9.3 使用了错误的密钥

检查 config 中是否遗漏 IdentitiesOnly yes,该配置可强制只使用指定密钥。

10. 总结

通过为 Gitee 和 GitHub 分别生成独立的 SSH 密钥,并在 ~/.ssh/config 中做好映射,即可在同一台电脑上无缝管理两个平台的仓库。核心要点:

  • 每个平台一把独立密钥;
  • config 文件按 Host 区分;
  • 公钥分别添加到对应平台;
  • 远程地址使用 git@平台域名:用户名/仓库名.git 格式。

配置一次,长期受益,从此在 Gitee 和 GitHub 之间自由切换,无需重复输入密码。

相关推荐
小弥儿8 小时前
GitHub今日热榜 | 2026-08-16:大模型微调与编码工具扎堆
学习·开源·github
fengyehongWorld9 小时前
Git 认证凭据管理
git
starp13 小时前
skill 管理,后缀为public可以被git commit
github
315356691314 小时前
DeepSeek Harness 发布后,我没急着跑 Demo,先把 `.agents/` 翻了一遍
前端·后端·github
u13013016 小时前
GitHub 热榜项目:周榜(2026-08-16)
github
暮云星影17 小时前
git版本发布
git·gitee·github·gitea
乌夷17 小时前
Git Flow 分支模型
git
m4Rk_17 小时前
【论文阅读】Agent 记忆机制(41):Agentic Plan Caching——将历史执行轨迹转化为可复用的计划记忆
论文阅读·人工智能·学习·开源·github