使用 SSH 密钥
当您使用 SSH 密钥而非凭据助手从 Git 仓库克隆项目时,需要确保本地 SSH agent 正在运行。
如果 SSH agent 正在运行,您可以使用 ssh-add 命令将本地 SSH 密钥添加到该代理中。
请查看以下示例:
ssh-add $HOME/.ssh/<your ssh key>
故障排除
在 Linux 和 Windows 上,可能会因为这些系统默认未启动 SSH agent 而出现错误。 在 macOS 上,SSH agent 默认处于运行状态。
要解决此类问题,请执行以下步骤:
确保以管理员身份运行本地 PowerShell。
输入以下命令:
Set-Service ssh-agent -StartupType Automatic Start-Service ssh-agent Get-Service ssh-agent
使用以下命令启动 SSH agent:
eval "$(ssh-agent -s)"如果您更倾向于使用
Zshshell 而不是bash,请将以下代码行添加到~/.bash_profile或~/.zprofile中。if [ -z "$SSH_AUTH_SOCK" ]; then # Check for a currently running instance of the agent RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`" if [ "$RUNNING_AGENT" = "0" ]; then # Launch a new instance of the agent ssh-agent -s > $HOME/.ssh/ssh-agent fi eval `cat $HOME/.ssh/ssh-agent` > /dev/null ssh-add $HOME/.ssh/<your ssh key> 2> /dev/null fi在建议代码的最后一行,将 <your ssh key> 替换为您的具体 ssh 密钥。 例如,
ssh-add $HOME/.ssh/id_ed344 2> /dev/null
最后修改日期: 2025年 9月 26日