github SSH

参考:https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

(GITHUB官网)

Before-check

  • 检查现有SSH密钥:
1
2
3
$ ls -al ~/.ssh

# Lists the files in your .ssh directory, if they exist

Check the directory listing to see if you already have a public SSH key. By default, the filenames of supported public keys for GitHub are one of the following.

  • id_rsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub

Generating a new SSH key

  • 打开Git Bash;
  • 生成SSH:
1
ssh-keygen -t ed25519 -C "your_email@example.com"

Adding your SSH key to the ssh-agent

  • In a new admin elevated PowerShell window, ensure the ssh-agent is running. You can use the “Auto-launching the ssh-agent” instructions in “Working with SSH key passphrases“, or start it manually.

auto version:

You can run ssh-agent automatically when you open bash or Git shell. Copy the following lines and paste them into your ~/.profile or ~/.bashrc file in Git shell:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
env=~/.ssh/agent.env

agent_load_env () { test -f "$env" && . "$env" > /dev/null ; }

agent_start () {
(umask 077; ssh-agent > "$env")
. "$env" > /dev/null ; }

agent_load_env

# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2=agent not running
agent_run_state=$(ssh-add -l > /dev/null 2>&1; echo $?)

if [ ! "$SSH_AUTH_SOCK" ] [ $agent_run_state = 2 ]; then
agent_start
ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add
fi

unset env

If your private key is not stored in one of the default locations (like ~/.ssh/id_rsa), you’ll need to tell your SSH authentication agent where to find it. To add your key to ssh-agent, type ssh-add ~/path/to/my_key

  • In a terminal window without elevated permissions, add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file.
1
ssh-add c:/Users/YOU/.ssh/id_ed25519

Adding a new SSH key to your GitHub account

Copy the SSH public key to your clipboard.

If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.

1
2
3
$ clip < ~/.ssh/id_ed25519.pub

# Copies the contents of the id_ed25519.pub file to your clipboard

然后进github复制粘贴就行了