All articles
git

How to Connect to GitHub with SSH Keys: A Simple Guide

Share this article

Share on LinkedIn Share on X (formerly Twitter)

If you are still typing your password every time you push code to GitHub, it is time to switch to SSH keys. SSH keys are a more secure and convenient way to authenticate, allowing you to sync your repositories without constantly logging in.

Why This Matters

  • No more passwords: Authenticate securely without typing your password every time.
  • Enhanced security: SSH keys are harder to crack than traditional passwords.
  • Scripting friendly: Essential for automating scripts and CI/CD pipelines.

Step 1: Check for Existing SSH Keys

Before generating a new key, check if you already have one:

# List all SSH keys
ls -al ~/.ssh

Look for files like id_rsa.pub, id_ecdsa.pub, or id_ed25519.pub. If you see one you want to use, you can skip to Step 3.

Step 2: Generate a New SSH Key

We recommend using Ed25519, which is modern and secure.

# Generate a new SSH key (replace with your email)
ssh-keygen -t ed25519 -C "your_email@example.com"
 
# When prompted, press Enter to accept the default file location
# Enter a passphrase if you want extra security (optional but recommended)

Step 3: Add the SSH Key to the SSH Agent

The SSH agent manages your keys and remembers your passphrase.

# Start the ssh-agent in the background
eval "$(ssh-agent -s)"
 
# Add your private key to the agent
ssh-add ~/.ssh/id_ed25519

Step 4: Add the SSH Key to GitHub

  1. Copy the public key to your clipboard:

    # Mac/Linux:
    cat ~/.ssh/id_ed25519.pub | pbcopy
     
    # Windows (Git Bash):
    cat ~/.ssh/id_ed25519.pub | clip
  2. Paste into GitHub:

    • Go to GitHub SSH Settings.
    • Click New SSH key.
    • Give it a title (e.g., "My Laptop").
    • Paste the key into the "Key" field.
    • Click Add SSH key.

Step 5: Test Your Connection

Verify that GitHub recognizes your key:

ssh -T git@github.com

You should see a message like:

Hi username! You've successfully authenticated, but GitHub does not provide shell access.


⚠️ Important Reminders

  1. Passphrases: If you set a passphrase, you will need to enter it once per session when you add the key to the agent.
  2. Key Types: If your system does not support Ed25519, use RSA: ssh-keygen -t rsa -b 4096.

Pro Tips

  • Use ssh-add -l to list all keys currently loaded in your agent.
  • If you have trouble connecting, run ssh -T git@github.com -v to see verbose debug logs.
  • Works for GitLab and BitBucket too!

You are now ready to push and pull code securely! 🎉


Comments