😤 Tired of This?
$ git push
Username for 'https://github.com': yourusername
Password for 'https://[email protected]':
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/yourusername/yourrepo.git/'
GitHub removed password authentication in 2021. Now you need either a Personal Access Token (PAT) or SSH keys. SSH keys are better—set them up once and never think about authentication again.
In this guide, I'll show you how to set up SSH keys for GitHub in 5 minutes. This works for GitLab, Bitbucket, and any other Git hosting service too.
Why SSH Keys Are Better Than Passwords
- More secure: SSH keys use cryptographic authentication, not passwords that can be guessed or stolen
- More convenient: Set up once, use forever—no typing passwords or tokens
- Faster: No authentication prompts slowing you down
- Works everywhere: GitHub, GitLab, Bitbucket, your own servers
Step-by-Step Setup
1 Check for Existing SSH Keys
First, let's see if you already have SSH keys:
ls -la ~/.ssh
Look for files named id_rsa.pub, id_ecdsa.pub, id_ed25519.pub, or similar. If you see these, you already have SSH keys and can skip to Step 3.
If you see "No such file or directory" or the folder is empty, continue to Step 2.
2 Generate a New SSH Key
Open your terminal and run:
ssh-keygen -t ed25519 -C "[email protected]"
💡 Why ed25519?
Ed25519 is the modern, secure algorithm. It's faster and more secure than the older RSA algorithm. If your system doesn't support ed25519, use ssh-keygen -t rsa -b 4096 -C "[email protected]" instead.
You'll be prompted for three things:
- File location: Press Enter to accept the default (
~/.ssh/id_ed25519) - Passphrase: Enter a strong passphrase (or press Enter for no passphrase)
- Confirm passphrase: Type it again
Generating public/private ed25519 key pair.
Enter file in which to save the key (/Users/you/.ssh/id_ed25519): [Press Enter]
Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type it again]
Your identification has been saved in /Users/you/.ssh/id_ed25519
Your public key has been saved in /Users/you/.ssh/id_ed25519.pub
⚠️ About Passphrases
A passphrase adds extra security, but you'll need to type it every time you use the key. For convenience, many developers skip the passphrase. If you do use one, consider using ssh-agent to remember it (see Step 5).
3 Add Your SSH Key to ssh-agent
The ssh-agent manages your keys and passphrases. Start it and add your key:
macOS:
# Start ssh-agent
eval "$(ssh-agent -s)"
# Add your key (macOS Keychain integration)
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Linux:
# Start ssh-agent
eval "$(ssh-agent -s)"
# Add your key
ssh-add ~/.ssh/id_ed25519
Windows (Git Bash):
# Start ssh-agent
eval "$(ssh-agent -s)"
# Add your key
ssh-add ~/.ssh/id_ed25519
4 Add Your SSH Key to GitHub
Now you need to tell GitHub about your public key.
Step 4a: Copy your public key to clipboard:
macOS:
pbcopy < ~/.ssh/id_ed25519.pub
Linux:
xclip -sel clip < ~/.ssh/id_ed25519.pub
# or
cat ~/.ssh/id_ed25519.pub # Then manually copy the output
Windows:
cat ~/.ssh/id_ed25519.pub # Then manually copy the output
Step 4b: Add to GitHub:
- Go to GitHub SSH Settings
- Click "New SSH key"
- Title: Give it a name (e.g., "MacBook Pro 2026")
- Key type: Authentication Key
- Key: Paste your public key (Ctrl+V or Cmd+V)
- Click "Add SSH key"
💡 Pro Tip
Use descriptive titles like "Work Laptop" or "Personal MacBook" so you know which device each key belongs to.
5 Test Your SSH Connection
Let's verify everything works:
ssh -T [email protected]
You should see:
The authenticity of host 'github.com (140.82.121.4)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
Are you sure you want to continue connecting (yes/no)? yes
Hi yourusername! You've successfully authenticated, but GitHub does not provide shell access.
Type yes and press Enter. If you see "You've successfully authenticated", you're all set!
6 Update Your Git Repositories
Now you need to switch your existing repositories from HTTPS to SSH.
For existing repositories:
cd /path/to/your/repo
# Check current remote URL
git remote -v
# If it shows https://github.com/..., change it to SSH
git remote set-url origin [email protected]:yourusername/yourrepo.git
# Verify the change
git remote -v
For new repositories:
# Clone using SSH URL (not HTTPS)
git clone [email protected]:yourusername/yourrepo.git
💡 How to Find the SSH URL
On any GitHub repository page, click the green "Code" button, then select "SSH" and copy the URL. It looks like: [email protected]:username/repo.git
Configure Git to Always Use SSH
Tell Git to automatically use SSH instead of HTTPS for GitHub:
git config --global url."[email protected]:".insteadOf https://github.com/
Now even if you copy an HTTPS URL, Git will automatically convert it to SSH.
Using SSH Keys with Multiple GitHub Accounts
If you have multiple GitHub accounts (personal and work), you need separate SSH keys for each.
Step 1: Generate a second key with a different name:
ssh-keygen -t ed25519 -C "[email protected]" -f ~/.ssh/id_ed25519_work
Step 2: Add both keys to ssh-agent:
ssh-add ~/.ssh/id_ed25519
ssh-add ~/.ssh/id_ed25519_work
Step 3: Create/edit ~/.ssh/config:
# Personal account
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
# Work account
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
Step 4: Use the appropriate host when cloning:
# Personal repos
git clone [email protected]:personaluser/repo.git
# Work repos (note the github-work host)
git clone git@github-work:workuser/repo.git
Troubleshooting Common Issues
"Permission denied (publickey)" Error
This means GitHub doesn't recognize your key. Check:
- Did you add the correct public key to GitHub?
- Is your key added to ssh-agent? Run
ssh-add -lto check - Are you using the correct email? Run
ssh -T [email protected]to verify
# Debug mode - shows detailed connection info
ssh -vT [email protected]
"Could not open a connection to your authentication agent"
ssh-agent isn't running. Start it:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
macOS: Key Keeps Asking for Passphrase
Add this to ~/.ssh/config:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
Security Best Practices
- Use a passphrase: Adds an extra layer of security
- Don't share your private key: Never commit
id_ed25519to a repository - Use different keys for different services: Don't use the same key for GitHub and your server
- Rotate keys periodically: Generate new keys every year or two
- Remove old keys: Delete unused keys from GitHub and your computer
Summary
You now have SSH keys set up for GitHub! Here's what you accomplished:
- ✅ Generated a secure ed25519 SSH key
- ✅ Added it to ssh-agent
- ✅ Configured GitHub to recognize your key
- ✅ Tested the connection
- ✅ Updated your repositories to use SSH
From now on, you can push and pull from GitHub without entering passwords or tokens. Enjoy the convenience!