
AI模型Deepseek仅供参考。第一步生成 SSH 密钥对在终端Windows 推荐用 Git Bash 或 PowerShell中运行ssh-keygen -t ed25519 -C your_emailexample.com将 your_emailexample.com 替换为你的 GitHub 注册邮箱。然后按提示操作- 保存路径直接回车使用默认路径 ~/.ssh/id_ed25519- 密码短语可选但推荐设置为私钥多加一层保护- 若 ed25519 算法不可用如系统过旧可改用 RSAssh-keygen -t rsa -b 4096 -C your_emailexample.com密钥文件说明~/.ssh/id_ed25519 是私钥留在本地绝对不要分享给任何人。~/.ssh/id_ed25519.pub 是公钥需要添加到 GitHub。第二步添加到 ssh-agent可选启动 SSH 代理并将私钥添加进去eval $(ssh-agent -s) # 启动 ssh-agentssh-add ~/.ssh/id_ed25519 # 添加私钥这样可以在当前会话中重用密钥避免重复输入密码。第三步将公钥添加到 GitHub1. 复制公钥- macOSpbcopy ~/.ssh/id_ed25519.pub- Linux需装 xclipxclip -selection clipboard ~/.ssh/id_ed25519.pub- Windows Git Bashcat ~/.ssh/id_ed25519.pub | clip- 通用方法cat ~/.ssh/id_ed25519.pub然后手动选中并复制所有输出2. 登录 GitHub 添加1) 登录 GitHub点击右上角头像 → Settings2) 左侧边栏点击 SSH and GPG keys3) 点击 New SSH key 按钮4) 填写信息- Title填写一个便于识别的名称如 My MacBook Pro 或 Office Desktop- Key type保持默认的 Authentication Key- Key粘贴刚刚复制的公钥内容5) 点击 Add SSH key按要求确认密码第四步测试连接在终端中运行ssh -T gitgithub.com首次连接会提示确认 host key输入 yes 继续。看到如下输出即表示成功Hi your-username! Youve successfully authenticated, but GitHub does not provide shell access.如果看到权限拒绝Permission denied错误请检查公钥是否完整粘贴、邮箱是否正确。第五步使用 SSH 协议操作仓库配置完成后即可使用 gitgithub.com: 格式的地址- 克隆新仓库git clone gitgithub.com:用户名/仓库名.git示例git clone gitgithub.com:octocat/Hello-World.gitSSH 地址可从仓库页面点击 Code → SSH 获取- 修改既有仓库将 HTTPS 地址改为 SSHgit remote set-url origin gitgithub.com:用户名/仓库名.git之后 git push、git pull 就不再需要每次输入密码了。常见问题排查- ssh-add 报 Could not open a connection先执行 eval $(ssh-agent -s) 启动代理- 公司网络封锁 22 端口改用 443 端口ssh -T -p 443 gitssh.github.com- Windows 环境没有 ssh-keygen安装 Git for Windows使用 Git Bash小建议一台设备配一个密钥就够了如果换新电脑重新走一遍流程即可。不再用的旧设备记得去 GitHub 删掉对应的密钥避免留下安全隐患。