Why Do You Need an SSH Key?
You might be wondering: Why do I even need an SSH Key? Well, an SSH key is like a VIP pass for your computer to access GitHub securely, without needing to type your password every time you use Git. It makes life easier and keeps your account safe.
Here's how it works:
- Your computer generates a pair of cryptographic keys: one public and one private.
- The public key is like a lock you share with GitHub, and the private key is the key that stays on your computer.
- When you try to access GitHub, your computer "unlocks" the connection using your private key. Simple and secure!
P.S: This means that you'll have to generate an SSH key for every device you plan on using Github with, or at least all the ones where you're going to work with a repo.
How to Generate and Add Your SSH Key
-
Open your terminal and enter the following command to generate your
key pair:
ssh-keygen -t ed25519 -C "your_email@example.com"
Replace
your_email@example.com
with the email you used for GitHub. -
When prompted to "Enter a file in which to save the key," press
Enter
to accept the default file location. -
When prompted for a passphrase, either enter one (for extra
security) or leave it blank and press
Enter
. -
Now, copy your public key to your clipboard:
cat ~/.ssh/id_ed25519.pub
Highlight the output and copy it (you'll need it for the next step).
- Go back to GitHub, click on your profile picture in the top-right corner, and navigate to Settings.
- Under the "Access" section in the sidebar, select SSH and GPG keys, then click New SSH key.
- Paste your public key into the text box, give it a recognizable title (e.g., "My Laptop" or "Macbook Pro [Name]"), and click Add SSH key.
-
To verify everything's working, go back to your terminal and run:
ssh -T git@github.com
If everything's set up correctly, you'll see a message like, "Hi [username]! You've successfully authenticated."