解决 Git SSH 密钥配置与冲突处理
问题诊断

密钥生成与配置
生成新密钥对
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/id_ed25519 -N "" -q
echo "--- 公钥内容 ---"
cat ~/.ssh/id_ed25519.pub
平台配置流程
- 登录Git托管平台
- 进入 Settings → SSH and GPG keys
- 点击 New SSH key
- 粘贴生成的公钥内容(以ssh-ed25519开头)
冲突解决方案
典型处理流程

标准操作命令集
git stash -u
git pull --rebase origin main
git stash pop
git push origin main
最佳实践建议
- 定期更新SSH密钥
- 为不同平台创建独立密钥对
- 使用
~/.ssh/config
文件管理多密钥 - 优先使用Ed25519算法而非RSA
配置
# ~/.ssh/config 配置模板
Host github.comHostName github.comUser gitIdentityFile ~/.ssh/id_workIdentitiesOnly yesHost gitlab.comHostName gitlab.comUser gitIdentityFile ~/.ssh/id_personal