使用 Ssh 连接到 Github

生成公私钥

1
ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/github

~/.ssh/config:

1
2
3
4
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/github

将公钥上传到 github

  • 公钥: ~/.ssh/github.pub
  • 上传到 github: Settings -> SSH and GPG keys

测试是否成功

1
ssh -T git@github.com

ssh over https

如果使用机场访问 github, 可以会出现无法连接的情况, 因为有些机场会屏蔽 22 端口以防止网络攻击。改为 https 协议即可。

1
2
3
4
5
Host github.com
  HostName ssh.github.com
  Port 443
  User git
  IdentityFile ~/.ssh/github

有多个 github 用户时

ssh config:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Host github.com
  HostName ssh.github.com
  Port 443
  User git
  IdentityFile ~/.ssh/github

Host github.com-<the other user name>
  HostName ssh.github.com
  Port 443
  User git
  IdentityFile ~/.ssh/github-joshua

clone:

1
2
git clone git@github.com:xxx/xxx.git
git clone git@github.com-<the other user name>:xxx/xxx.git

参考

0%