配置SSH密钥统一推送Github和Gitee

1. 为 Github 生成 SSH 密钥

ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/id_ed_github

将 id_ed_github.pub 添加到 Github SSH keys中。Settings -> Access ->SSH and GPG keys -> New SSH keys

2. 为 Gitee 生成 SSH 密钥

ssh-keygen -t ed25519 -C "your_gitee_email@example.com" -f ~/.ssh/id_ed_gitee

将 id_ed_gitee.pub 添加到 Gitee SSH 公钥中。设置 -> 安全设置 -> SSH 公钥

参考:Gitee SSH 公钥设置

3. 配置 SSH 的 config 文件

# Github
Host github.com
  HostName github.com
  Port 22
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed_github
  IdentitiesOnly yes

# Gitee
Host gitee.com
  HostName gitee.com
  Port 22
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed_gitee
  IdentitiesOnly yes

ssh_config(5): https://man.openbsd.org/ssh_config

4. Git 添加 remote

git remote add github git@github.com:username/repo.git
git remote add gitee git@gitee.com:username/repo.git

git remote -v
gitee   git@gitee.com:username/repo.git (fetch)
gitee   git@gitee.com:username/repo.git (push)
github  git@github.com:username/repo.git (fetch)
github  git@github.com:username/repo.git (push)

git-remote: https://git-scm.com/docs/git-remote

5. 测试

出现 You've successfully authenticated,表示成功。

# 测试 Github
ssh -T git@github.com
The authenticity of host 'github.com (20.205.243.166)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
Hi geyashi! You've successfully authenticated, but GitHub does not provide shell access

# 测试 Gitee
ssh -T git@gitee.com
The authenticity of host 'gitee.com (180.76.199.13)' can't be established.
ED25519 key fingerprint is SHA256:+ULzij2u99B9eWYFTw1Q4ErYG/aepHLbu96PAUCoV88.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com' (ED25519) to the list of known hosts.
Hi weiweqi(***)! You've successfully authenticated, but GITEE.COM does not provide shell access.

6. 推送

git push github master
git push gitee master
posted @ 2025-11-22 17:11  geyashi  阅读(2)  评论(0)    收藏  举报