Git SSH 配置指南

Git SSH 配置指南

一、Git 基础配置

git config --global user.name "你的名字"
git config --global user.email "你的邮箱@example.com"

# 验证配置
git config --global --list

二、生成 SSH 密钥

# 推荐:ed25519(更安全、更短)
ssh-keygen -t ed25519 -C "你的邮箱@example.com"

# 备用:rsa(旧系统兼容)
ssh-keygen -t rsa -b 4096 -C "你的邮箱@example.com"

执行后三次提示:

Enter file in which to save the key:   ← 回车使用默认路径
Enter passphrase:                       ← 设置密钥密码(强烈推荐)
Enter same passphrase again:            ← 再输一次确认

生成的文件:

C:\Users\`%username%`\.ssh\
├── id_ed25519        ← 私钥(绝对不能泄露)
└── id_ed25519.pub    ← 公钥(上传到 GitHub/GitLab)

给已有私钥添加/修改密码

ssh-keygen -p -f $env:USERPROFILE\.ssh\id_ed25519
# Enter old passphrase: ← 没有旧密码直接回车
# Enter new passphrase: ← 输入新密码

三、配置 ssh-agent

ssh-agent 在后台托管私钥,开机输一次密码,之后 git 操作无需重复输入。

# 设置开机自启(管理员 PowerShell 执行一次)
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent

# 将私钥加入 agent(输一次密码)
ssh-add $env:USERPROFILE\.ssh\id_ed25519

# 验证是否加载成功
ssh-add -l

四、上传公钥到平台

# 复制公钥内容到剪贴板
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard
  • GitHub:头像 → Settings → SSH and GPG keys → New SSH key → 粘贴 → Add SSH key
  • GitLab:头像 → Preferences → SSH Keys → 粘贴 → Add key

五、验证连接

ssh -T git@github.com
# 成功:Hi 用户名! You've successfully authenticated...

ssh -T git@gitlab.com
# 成功:Welcome to GitLab, 用户名!

六、管理多个 SSH 密钥

当需要同时使用多个平台(GitHub、GitLab、公司服务器)时,为每个平台生成独立密钥。

1. 生成多个密钥(命名区分)

# GitHub 密钥
ssh-keygen -t ed25519 -C "邮箱@example.com" -f $env:USERPROFILE\.ssh\id_ed25519_github

# GitLab 密钥
ssh-keygen -t ed25519 -C "邮箱@example.com" -f $env:USERPROFILE\.ssh\id_ed25519_gitlab

# 公司服务器密钥
ssh-keygen -t ed25519 -C "邮箱@company.com" -f $env:USERPROFILE\.ssh\id_ed25519_work

目录结构:

C:\Users\`%username%`\.ssh\
├── id_ed25519_github
├── id_ed25519_github.pub
├── id_ed25519_gitlab
├── id_ed25519_gitlab.pub
├── id_ed25519_work
├── id_ed25519_work.pub
└── config                ← 关键:SSH 路由配置文件

2. 创建 SSH config 文件

C:\Users\%username%\.ssh\config 创建以下内容:

# GitHub
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github
    IdentitiesOnly yes

# GitLab
Host gitlab.com
    HostName gitlab.com
    User git
    IdentityFile ~/.ssh/id_ed25519_gitlab
    IdentitiesOnly yes

# 公司服务器(自定义别名)
Host work
    HostName 192.168.1.100
    User ubuntu
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

3. 将所有私钥加入 ssh-agent

ssh-add $env:USERPROFILE\.ssh\id_ed25519_github
ssh-add $env:USERPROFILE\.ssh\id_ed25519_gitlab
ssh-add $env:USERPROFILE\.ssh\id_ed25519_work

# 查看已加载的所有密钥
ssh-add -l

4. 分别上传各平台公钥

# 复制 GitHub 公钥
Get-Content $env:USERPROFILE\.ssh\id_ed25519_github.pub | Set-Clipboard

# 复制 GitLab 公钥
Get-Content $env:USERPROFILE\.ssh\id_ed25519_gitlab.pub | Set-Clipboard

5. 验证各平台连接

ssh -T git@github.com
ssh -T git@gitlab.com
ssh -T work          # 使用 config 中定义的别名

七、让 Git 使用 Windows 内置 OpenSSH(重要)

Scoop 安装的 Git 自带一套 SSH 客户端(Git Bash 内置),它与 Windows ssh-agent 互不相通
导致即使配置了 ssh-agent,克隆时仍会提示输入密码。

需要手动指定 Git 使用 Windows 系统的 OpenSSH:

git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"

原理说明:

Scoop Git 自带 SSH(/c/Users/... Unix 风格路径)
    ↓ 不认识 Windows ssh-agent → 每次提示输密码

Windows OpenSSH(C:\Windows\System32\OpenSSH\ssh.exe)
    ↓ 与 Windows ssh-agent 无缝集成 → 不再提示密码

验证配置是否生效:

git config --global core.sshCommand
# 输出:C:/Windows/System32/OpenSSH/ssh.exe

八、日常 Git 使用

# 克隆项目(使用 SSH 地址)
git clone git@github.com:用户名/仓库名.git

# 已有项目,HTTPS 地址改为 SSH
git remote set-url origin git@github.com:用户名/仓库名.git
git remote -v   # 验证

九、常用命令速查

ssh-add -l                                            # 查看 agent 中已加载的密钥
ssh-add -D                                            # 清除 agent 中所有密钥
ssh-keygen -p -f ~/.ssh/id_ed25519                    # 修改私钥密码
ssh -T git@github.com                                 # 测试 GitHub 连接
git config --global --list                            # 查看全局 git 配置
git config --global core.sshCommand                   # 查看当前 SSH 客户端路径
posted @ 2026-04-04 15:55  DM学编程  阅读(5)  评论(0)    收藏  举报