使用 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
12
13
#git config --global url."git@github-foo.com:foo/".insteadOf "git@github.com:foo/" 
#git config --global url."git@github-bar.com:bar/".insteadOf "git@github.com:bar/" 
Host github-foo.com
  HostName ssh.github.com
  Port 443
  User git
  IdentityFile ~/.ssh/github-foo

Host github-bar.com
  HostName ssh.github.com
  Port 443
  User git
  IdentityFile ~/.ssh/github-bar

参考

目录