Introduction

SSH authentication is a secure and convenient way to interact with GitHub repositories without needing to enter your credentials every time you push or pull changes. This guide will walk you through setting up SSH authentication with GitHub, generating an SSH key, adding it to GitHub, and configuring your Git client to use SSH.

1. Check for an Existing SSH Key

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

ls -al ~/.ssh

If you see files like id_rsa and id_rsa.pub (or id_ed25519 and id_ed25519.pub), you already have an SSH key.

2. Generate a New SSH Key (If Needed)

If you don’t have an SSH key or want to generate a new one, run:

ssh-keygen -t ed25519 -C "your-email@example.com"
  • Replace your-email@example.com with your GitHub email.
  • Press Enter to accept the default file location (~/.ssh/id_ed25519).
  • Set a passphrase for additional security (or leave it blank).

3. Add the SSH Key to the SSH Agent

To ensure the SSH key is used, add it to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519  # Use id_rsa if you generated an RSA key

4. Add the SSH Key to GitHub

  1. Copy your SSH public key:
   cat ~/.ssh/id_ed25519.pub  # Or `cat ~/.ssh/id_rsa.pub`
  1. Go to GitHub → Settings → SSH and GPG Keys

    👉 GitHub SSH Key Settings
  2. Click "New SSH Key", enter a title (e.g., "Work Laptop"), paste your key, and click "Add SSH Key".

5. Test Your SSH Connection

To verify that SSH authentication is working, run:

ssh -T git@github.com

If successful, you should see a message like:

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

6. Configure Git to Use SSH

If your repository was cloned using HTTPS, update the remote URL to use SSH:

git remote set-url origin git@github.com:your-username/repository-name.git

Check that the change was applied:

git remote -v

The output should now show:

origin  git@github.com:your-username/repository-name.git (fetch)
origin  git@github.com:your-username/repository-name.git (push)

7. Push Changes Using SSH

Now that SSH is set up, you can push and pull changes without entering credentials:

git push origin main

Conclusion

SSH authentication with GitHub enhances security and convenience by allowing secure, password-free interactions with your repositories. Once set up, SSH eliminates the need for Personal Access Tokens (PATs) required for HTTPS authentication.

By following these steps, you can configure SSH authentication, add your key to GitHub, and securely manage your repositories.

Troubleshooting

  • If you encounter Permission denied (publickey), ensure:
    • Your SSH key is added to GitHub.
    • The SSH agent is running (ssh-add -l should list your key).
    • Your remote URL is correctly set to use SSH (git remote -v).

Let me know when you get stuck in between the tutorial

Author Of article : Sospeter Mong'are Read full article