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

相关推荐
时光追逐者3 小时前
Visual Studio使用GitHub Copilot提高.NET开发工作效率
c#·github·.net·copilot·ai编程·微软技术·visual studio
油泼辣子多加11 小时前
2025年02月01日Github流行趋势
github
萨文 摩尔杰11 小时前
hexo部署到github page时,hexo d后page里面绑定的个人域名消失的问题
github
油泼辣子多加1 天前
2025年01月31日Github流行趋势
github
Ronin-Lotus2 天前
上位机知识篇---Git&GitHub
git·学习·github
AI悦创|编程1v12 天前
GitHub Actions定时任务配置完全指南:从Cron语法到实战示例
github
**kwargs2 天前
ssh密钥登录GitHub时一直提示“Error: Permission denied (publickey)”
运维·ssh·github
dreadp2 天前
解锁豆瓣高清海报:深度爬虫与requests进阶之路
前端·爬虫·python·beautifulsoup·github·requests
江_小_白3 天前
fatal: unable to access ‘https://github
github
小华同学ai3 天前
Van-Nav:新年,将自己学习的项目地址统一整理搭建自己的私人导航站,供自己后续查阅使用,做技术的同学应该都有一个自己网站的梦想
学习·gitee·开源·github