Skip
Arish's avatar

11. Add SSH Key to GitHub Account


SSH Keys for GitHub

SSH keys allow you to connect to GitHub without entering your username and password every time.

Why Use SSH?

MethodProsCons
HTTPSEasy setupEnter credentials often
SSHNo passwords, secureInitial setup required

Step 1: Check for Existing SSH Keys

bash
1# List existing SSH keys
2ls -la ~/.ssh
3
4# Look for files like:
5# id_ed25519 / id_ed25519.pub
6# id_rsa / id_rsa.pub

If you see existing keys, you can skip to Step 3.

Step 2: Generate a New SSH Key

bash
1ssh-keygen -t ed25519 -C "your.email@example.com"

Using RSA (If Ed25519 not supported)

bash
1ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

Prompts

Generating public/private ed25519 key pair. Enter file in which to save the key (/home/you/.ssh/id_ed25519): [Press Enter] Enter passphrase (empty for no passphrase): [Type passphrase or Enter] Enter same passphrase again: [Confirm passphrase]

Tip: A passphrase adds extra security but is optional.

Output

Your identification has been saved in /home/you/.ssh/id_ed25519 Your public key has been saved in /home/you/.ssh/id_ed25519.pub The key fingerprint is: SHA256:abc123... your.email@example.com

Step 3: Add SSH Key to SSH Agent

Start the SSH Agent

bash
1# Mac/Linux
2eval "$(ssh-agent -s)"
3# Output: Agent pid 12345
4
5# Windows (Git Bash)
6eval "$(ssh-agent -s)"

Add Your Key

bash
1# Mac
2ssh-add --apple-use-keychain ~/.ssh/id_ed25519
3
4# Linux/Windows
5ssh-add ~/.ssh/id_ed25519

Step 4: Copy the Public Key

Mac

bash
1pbcopy < ~/.ssh/id_ed25519.pub

Linux

bash
1cat ~/.ssh/id_ed25519.pub
2# Then manually copy the output

Windows (Git Bash)

bash
1clip < ~/.ssh/id_ed25519.pub

The public key looks like:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIH... your.email@example.com

Step 5: Add Key to GitHub

  1. Go to github.com and log in
  2. Click your profile pictureSettings
  3. Click SSH and GPG keys in the sidebar
  4. Click New SSH key
  5. Fill in the form:
    • Title: "My Laptop" (or any descriptive name)
    • Key type: Authentication Key
    • Key: Paste your public key
  6. Click Add SSH key
  7. Confirm with your GitHub password if prompted

Step 6: Test the Connection

bash
1ssh -T git@github.com

First Time Connection

The authenticity of host 'github.com (IP ADDRESS)' can't be established. ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Type yes and press Enter.

Successful Connection

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

🎉 You're connected!

Using SSH with Git

Clone with SSH

bash
1git clone git@github.com:username/repository.git

Change Existing Remote to SSH

bash
1# Check current remote
2git remote -v
3
4# Change from HTTPS to SSH
5git remote set-url origin git@github.com:username/repository.git
6
7# Verify
8git remote -v

Managing Multiple SSH Keys

If you have multiple GitHub accounts:

Create Config File

bash
1# Create or edit ~/.ssh/config
2nano ~/.ssh/config

Add Configurations

# Personal GitHub Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519_personal # Work GitHub Host github-work HostName github.com User git IdentityFile ~/.ssh/id_ed25519_work

Clone with Specific Key

bash
1# Personal
2git clone git@github.com:personal/repo.git
3
4# Work
5git clone git@github-work:company/repo.git

Troubleshooting

Permission Denied (publickey)

bash
1# Check if SSH agent has your key
2ssh-add -l
3
4# If empty, add your key
5ssh-add ~/.ssh/id_ed25519
6
7# Verify the key is on GitHub
8ssh -T git@github.com

Wrong Key Being Used

bash
1# Test with specific key
2ssh -i ~/.ssh/id_ed25519 -T git@github.com

Summary

You've learned how to:

  • ✅ Generate SSH keys
  • ✅ Add keys to SSH agent
  • ✅ Add public key to GitHub
  • ✅ Test the connection
  • ✅ Use SSH for Git operations

SSH authentication is now set up for secure, password-free Git operations!