SourceTree配置SSH步骤详解
1. 生成SSH密钥对
如果尚未生成SSH密钥,需先创建:
Windows/macOS/Linux通用方法
-
打开终端(或Git Bash)。
-
输入以下命令(替换为你的邮箱):
bash
复制
ssh-keygen -t ed25519 -C "your_email@example.com"
-
按回车确认默认保存路径(通常为
~/.ssh/id_ed25519
)。 -
设置一个安全的密码(可选,但建议)。
-
2. 将公钥添加到Git托管服务
-
公钥文件:
~/.ssh/id_ed25519.pub
(用文本编辑器打开并复制内容)。 -
添加到平台:
-
GitHub: Settings → SSH and GPG Keys → New SSH Key
-
GitLab: Preferences → SSH Keys
-
Bitbucket: Personal Settings → SSH Keys
-
3. 配置SourceTree使用SSH
步骤 1:设置SSH客户端
-
打开SourceTree,进入 工具 → 选项(Options)。
-
切换到 一般(General) 标签页。
-
在 SSH客户端配置 处:
-
Windows:选择 OpenSSH 并指定路径(如
C:\Program Files\Git\usr\bin\ssh.exe
)。 -
macOS:选择 使用系统SSH(默认路径
/usr/bin/ssh
)。
-
步骤 2:添加SSH密钥到SourceTree
-
进入 工具 → 选项 → 一般。
-
点击 SSH配置 区域的 添加 按钮。
-
选择你的私钥文件(如
id_ed25519
),输入密码(如果生成时设置了)。
4. 验证SSH连接
在终端运行以下命令测试连接(以GitHub为例):
bash
复制
ssh -T git@github.com
-
成功会显示:
Hi YourUsername! You've successfully authenticated.
5. 克隆仓库或修改现有仓库URL
-
克隆仓库:在SourceTree中使用SSH格式的仓库地址(如
git@github.com:user/repo.git
)。 -
修改现有仓库URL:
-
右键仓库 → 仓库设置(Repository Settings)。
-
在 远程仓库 中修改URL为SSH格式。
-
常见问题排查
-
权限错误:确保私钥文件权限为
600
:bash
复制
chmod 600 ~/.ssh/id_ed25519
-
多密钥配置:编辑
~/.ssh/config
文件,指定不同Host对应不同密钥:复制
Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_github
完成以上步骤后,SourceTree即可通过SSH与远程仓库交互。如果仍有问题,检查SSH配置路径或重新启动SourceTree。