Connect to GitHub through ssh
To connect to GitHub through ssh you need to follow these steps:
If you don’t already have an SSH key, go to generate a new SSH key section. If you’re unsure whether you already have an SSH key, read check for existing keys section.
Check for existing keys
- Open Git Bash on Windows or Terminal on Mac.
Enter ls -al ~/.ssh to see if existing SSH keys are present:
|$ 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 the public keys are one of the following:
If you don’t have an existing public and private key pair or don’t wish to use any that are available to connect to GitHub, then jump to generate a new SSH key (next section).
If you see an existing public and private key pair listed (for example id_rsa.pub and id_rsa) that you would like to use to connect to GitHub, you can add your SSH key to the ssh-agent (next 2 sections).
Generating a new SSH key
- Open Git Bash on Windows or Terminal on Mac..
- Paste the text below, substituting in your GitHub email address.
|$ ssh-keygen -t rsa -b 4096 -C “email@example.com”|
This creates a new ssh key, using the provided email as a label.
|> Generating public/private rsa key pair.|
- When you’re prompted to “Enter a file in which to save the key,” press Enter. This accepts the default file location.
|> Enter a file in which to save the key (/c/Users/you/.ssh/id_rsa):[Press enter]|
- At the prompt, type a secure passphrase. Leave it empty.
|> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
It is done. You have a new SSH key in the specified folder. You need it for next 2 sections.
Adding SSH key to the ssh-agent
To add your SSH key to the ssh-agent follow these steps:
- Start ssh-agent in the background.
|$ eval $(ssh-agent -s)
> Agent pid 59566
- 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_rsa in the command with the name of your private key file.
|$ ssh-add ~/.ssh/id_rsa|
Great. Now you have added SSH key.
Adding the SSH key to your GitHub account
1.Copy the SSH key to your clipboard. If your SSH 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.
|$ clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
Tip: If clip command isn’t working, you can locate the hidden .ssh folder, open the file in your favorite text editor, and copy it to your clipboard.
- In the upper-right corner of any page, click your profile photo, then click Settings.
- In the user settings sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- In the “Title” field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key “Personal MacBook Air”.
- Paste your key into the “Key” field.
- Click Add SSH key.
8. If prompted, confirm your GitHub password.