构建需要密码时候使用SSH密钥认证
- 生成密匙
ssh-keygen -t ed25519 -C "your_email@example.com"
添加公钥到 GitHub:
# 查看公钥
cat ~/.ssh/id_ed25519.pub
# 复制到 GitHub: Settings → SSH and GPG keys → New SSH key
修改远程仓库为 SSH:
# 查看当前远程仓库地址
git remote -v
# 修改为 SSH 地址
git remote set-url origin git@github.com:你的用户名/你的仓库名.git
# 确认修改成功
git remote -v
这个错误说明 SSH 代理没有运行。需要启动 SSH 代理并添加密钥。
- 启动 SSH 代理
# 启动 SSH 代理
eval "$(ssh-agent -s)"
# 或者明确指定
ssh-agent bash
- 添加密钥到 SSH 代理
# 添加默认密钥
ssh-add
# 或者明确指定密钥文件
ssh-add ~/.ssh/id_rsa
# 或者如果是 ed25519 密钥
ssh-add ~/.ssh/id_ed25519
- 确认 SSH 代理是否成功添加密钥
ssh-add -l
完整的设置流程
# 1. 启动 SSH 代理
eval "$(ssh-agent -s)"
# 2. 添加你的 SSH 密钥
ssh-add ~/.ssh/id_rsa
# 或者
ssh-add ~/.ssh/id_ed25519
# 3. 验证
ssh-add -l
# 4. 测试 GitHub 连接
ssh -T git@github.com
修改远程仓库地址
# 查看当前远程地址
git remote -v
# 修改为 SSH(替换为你的实际用户名和仓库名)
git remote set-url origin git@github.com:你的用户名/你的仓库名.git
# 确认修改
git remote -v
以上步骤完成后,你应该能够通过 SSH 密钥认证成功连接到 GitHub,并且不再需要输入密码。