在项目开发中,使用 SSH 连接 Git 仓库可以提高安全性和便利性。本文将指导你如何将 Git 远程 URL 从 HTTPS 更改为 SSH。
操作指南
步骤 1: 查看当前远程 URL
首先,确认当前的远程 URL 使用的是 https。打开终端并输入以下命令:
git remote -v
如:
origin https://github.com/username/repo.git (fetch)
origin https://github.com/username/repo.git (push)
步骤 2: 修改远程 URL 为 SSH
使用以下命令将远程 URL 更改为 ssh:
git remote set-url origin git@github.com:username/repo.git
步骤3:配置 ssh 密钥
检查本地 ssh 密钥
ls ~/.ssh
如果没有,生成一个新的
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

复制 ssh 公钥到 github
cat ~/.ssh/id_rsa.pub
