Whenever you need to connect to a private Git repository you need to authenticate your machine with GitHub. One way to do this is to add the SSH key to the GitHub account with the Git repositories.
Generate the SSH Key
If you don’t have an SSH key already on your machine or you would rather generate a new SSH key to use especially for GitHub you can follow the How to generate an SSH key guide.
Add SSH Key to GitHub account
Log in to your GitHub account and then go to the Settings page then from the left-hand menu click on the SSH and GPG keys. Alternatively, you go to https://github.com/settings/keys directly via a direct URL.
On the settings page click the green New SSH Key button to go to the Add new SSH key page to add your key.
Go to the directory where your public key is which is the .pub file and copy it. You could use the cat command to output the key and copy it from the terminal.
cat github.pub
Go back to the Add new SSH key page and add the key to the Key text area, enter a name in the Title text field and then press the green Add SSH key button.
You will be required to authenticate the GitHub account to add the new SSH key.
The key will appear in the list of SSH keys on the key settings page.
Use SSH key to connect to GitHub
Create a config file if it doesn’t exist or edit the existing config file.
nano ~/.ssh/config
Add the following content to it, replace GitHub with the name of the SSH key added to your GitHub account.
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/github
Verify SSH key
Run the following to check that your Git account can authenticate with GitHub.
ssh -T [email protected]
If you see the following it worked.
Hi meshu-dev! You've successfully authenticated, but GitHub does not provide shell access.
Add username and email address
When committing changes to existing projects you will require a username and email setup in your Git configuration.
Use the following to set your username.
git config --global user.name "Mesh"
And also run the following to set your email address.
git config --global user.email "[email protected]"