1)先检查你机器上有没有现成 SSH key
ls -al ~/.ssh
看看有没有这类文件:
id_ed25519
id_ed25519.pub
如果你已经有 id_ed25519.pub,其实可以直接复用;没有的话就继续下一步。GitHub 官方也是先建议检查已有密钥。
2)生成新的 SSH key
推荐用 ed25519。GitHub 文档当前示例也是这个。
把下面命令里的邮箱换成你的 GitHub 邮箱:
ssh-keygen -t ed25519 -C "你的GitHub邮箱"
这里一直直接按 回车,用默认路径就行。
~/.ssh/id_ed25519
~/.ssh/id_ed25519.pub
3)启动 ssh-agent,并把私钥加进去
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
4)把公钥内容复制出来
执行:
cat ~/.ssh/id_ed25519.pub
会输出一整行,类似这样:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI......
把整行完整复制
5)把公钥加到 GitHub 账号
https://github.com/settings/keys
在 GitHub 网页里操作:
- 打开 GitHub
- 右上角头像
Settings- 左侧找到
SSH and GPG keys - 点
New SSH key或Add SSH key Title随便写一个你认得出的名字,比如:WSL-LaptopUbuntu-local
Key type选 Authentication KeyKey里粘贴刚才复制的整行公钥- 点
Add SSH key
6)测试 SSH 是否连通 GitHub
执行:
ssh -T git@github.com
会输出:
Hi 你的GitHub用户名! You've successfully authenticated...
GitHub 官方文档也是用这个命令测试连接,并说明成功信息里会包含你的用户名。
7)用 SSH 地址克隆仓库
你原来用的是 HTTPS:
git clone https://github.com/HongxinXiang/visualdna.git
改成 SSH:
git clone git@github.com:HongxinXiang/visualdna.git
这样后面通常就不会再让你输入 GitHub 用户名和密码了。GitHub 官方说明,仓库可以用 SSH URL 来 clone。