为什么要用 SSH Key?

使用 HTTPS 连接 GitHub 每次推送都需要输入账号密码(或 Token),而配置 SSH Key 之后,完全免密操作,更安全也更方便。

Why Use SSH Key?

HTTPS requires entering your credentials every push. With SSH Key configured, you get password-free access — more secure and convenient.


第一步:检查是否已有 SSH Key

在终端运行:

ls ~/.ssh

如果看到 id_rsa.pubid_ed25519.pub,说明已经有 Key 了,跳到第三步。

If you see id_rsa.pub or id_ed25519.pub, you already have a key — skip to Step 3.


第二步:生成 SSH Key

运行以下命令(把邮箱替换成你的 GitHub 注册邮箱):

ssh-keygen -t ed25519 -C "[email protected]"

全程按回车使用默认设置即可(不需要设置密码)。

Press Enter through all prompts to use defaults (no passphrase needed).

生成完成后会有两个文件:

  • ~/.ssh/id_ed25519(私钥,不要泄露
  • ~/.ssh/id_ed25519.pub(公钥,上传到 GitHub)

第三步:复制公钥内容

Mac / Linux:

cat ~/.ssh/id_ed25519.pub

Windows PowerShell:

cat ~/.ssh/id_ed25519.pub

复制输出的全部内容(以 ssh-ed25519 开头)。

Copy the entire output (starts with ssh-ed25519).


第四步:添加到 GitHub

  1. 登录 GitHub,点右上角头像 → Settings
  2. 左侧菜单点 SSH and GPG keys
  3. New SSH key
  4. Title 填写备注名(如 My Laptop
  5. Key 栏粘贴刚才复制的公钥内容
  6. Add SSH key

第五步:测试连接

看到以下提示说明配置成功:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.

第六步:切换仓库为 SSH 地址

已有的仓库需要把 remote 地址从 HTTPS 改为 SSH:

git remote set-url origin [email protected]:username/repository.git

之后 git push 就完全免密了。


常见问题 | FAQ

Q: Permission denied (publickey) 错误
A: 检查公钥是否正确添加到 GitHub,或重新运行 ssh -T [email protected]

Q: Windows 上找不到 ~/.ssh 目录
A: ~ 对应 C:\Users\你的用户名\,即 C:\Users\你的用户名\.ssh\


下一步 | Next Steps