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

相关推荐
m0_694845577 小时前
tinylisp 是什么?超轻量 Lisp 解释器编译与运行教程
服务器·开发语言·云计算·github·lisp
June`7 小时前
muduo项目排查错误+测试
linux·c++·github·muduo网络库
weixin_6689 小时前
GitHub 2026年AI项目详细数据汇总表-AI分析-分享
人工智能·github
CoderJia程序员甲1 天前
GitHub 热榜项目 - 日榜(2026-02-05)
ai·开源·大模型·github·ai教程
weixin_6681 天前
GitHub 2026年AI项目热度分析报告-AI分析-分享
人工智能·github
MicrosoftReactor1 天前
技术速递|GitHub Copilot CLI 斜杠命令速查表
github·copilot·cli
wu~9701 天前
GitHub永不遗忘,使用git push -f来覆盖的提交依旧保留
git·github
m0_694845571 天前
music-website 是什么?前后端分离音乐网站部署实战
linux·运维·服务器·云计算·github
独自破碎E1 天前
已经 Push 到远程的提交,如何修改 Commit 信息?
开发语言·github
jiang_changsheng1 天前
工作流agent汇总分析 2
java·人工智能·git·python·机器学习·github·语音识别