It’s a pain having to enter your username and password each time you pull/push from a Git repo. Let’s learn how to setup your SSH key on you computer and connect it with GitHub to avoid having to enter your credentials.
- Generate a new SSH key on your computer (if you don’t have one already) with the command
ssh-keygen -t rsa -b 2048 -C "email@example.com"
. You can hit enter for all of the prompts unless you want to password protect your key (recommended). - Now that you have your key generated, let’s view the public key with the command:
cat ~/.ssh/id_rsa.pub
(change the path to where you saved the key) - Copy the output of the above command which will start with ‘ssh-rsa’ and end with your email.
- Go into your GitHub account settings (or GitLab), select ‘SSH and GPG keys’ and click ‘New SSH Key’
- Give your key a title (ex. Macbook) and paste the contents you copied earlier in the box below labeled ‘Key’
- Now your key is connected to GitHub! When cloning repos in the future make sure to copy the ‘SSH’ URL which should start with ‘git@github’…
- If you already have repos cloned with HTTPS and you want to change them to use your newly generated key, run the command in each repo:
git remote set-url origin SSH_ORIGIN_COPIED_FROM_GITHUB
Feel free to leave comments or questions below!
If you prefer video tutorials, checkout my Git SSH key setup video below: