1.git bash 生成密钥
在 Git Bash 中生成 SSH 密钥的完整步骤如下(适用于 GitHub、GitLab、Hugging Face 等平台):
1. 打开 Git Bash
在 Windows 上右键点击文件夹,选择 Git Bash Here。
2. 生成 SSH 密钥对
运行以下命令(替换 your_email@example.com
为你的邮箱):
ssh-keygen -t ed25519 -C "your_email@example.com"
-t ed25519
:使用更安全的 Ed25519 算法(推荐)
如果系统不支持,改用 RSA:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
3. 设置保存路径
按提示操作:
Enter file in which to save the key (/c/Users/YourName/.ssh/id_ed25519):
-
直接按回车使用默认路径(
~/.ssh/id_ed25519
)。
4. 设置密码(可选)
Enter passphrase (empty for no passphrase):
-
输入密码(增加安全性,每次使用密钥需输入)
-
若不需要密码,直接按回车跳过。
5. 生成成功
输出类似:
Your identification has been saved in /c/Users/YourName/.ssh/id_ed25519
Your public key has been saved in /c/Users/YourName/.ssh/id_ed25519.pub
6. 将公钥添加到平台
(1) 复制公钥内容
cat ~/.ssh/id_ed25519.pub
或使用 clip
命令复制到剪贴板:
cat ~/.ssh/id_ed25519.pub | clip
(2) 添加到目标平台
-
GitHub:Settings → SSH and GPG keys → New SSH key
-
GitLab:Preferences → SSH Keys
-
Hugging Face:Settings → SSH Keys
粘贴公钥内容(以 ssh-ed25519 AAA...
开头)。
7. 测试连接
ssh -T git@github.com # 测试 GitHub
或针对其他平台:
ssh -T git@huggingface.co # Hugging Face
首次连接会提示确认,输入 yes
。
8. 配置 Git 使用 SSH
git config --global user.name "YourName"
git config --global user.email "your_email@example.com"
git config --global core.sshCommand "ssh -i ~/.ssh/id_ed25519" # 指定密钥路径
常见问题解决
问题1:权限错误
chmod 600 ~/.ssh/id_ed25519*
chmod 700 ~/.ssh
问题2:代理冲突
如果使用代理,在 ~/.ssh/config
中添加:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
ProxyCommand connect -H proxy.server.com:8080 %h %p
密钥管理建议
-
备份密钥:将
~/.ssh/
下的密钥文件保存到安全位置。 -
多平台密钥:不同平台建议使用不同密钥对(通过
-f
指定不同文件名)。