This guide explains how to set up SSH authentication for GitHub, which is especially important when multi-factor authentication (MFA) is enabled.
If you don't already have an SSH key, generate one:
ssh-keygen -t ed25519 -C "your.email@example.com"Notes:
- Press Enter to accept the default location (
~/.ssh/id_ed25519) - Optionally set a passphrase for additional security
- Replace
your.email@example.comwith your actual email address
Display and copy your public key:
cat ~/.ssh/id_ed25519.pubThe output will look something like:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJl3dIeudNqd0DPMRD6OIh65tjkxFNOtwGcWB2gCgPhk your.email@example.com
- Go to GitHub → Settings → SSH and GPG keys
- Click "New SSH key"
- Give it a descriptive title (e.g., "Development Container" or "Work Laptop")
- Paste your public key in the "Key" field
- Click "Add SSH key"
Verify that your SSH key is working:
ssh -T git@github.comYou should see a message like:
Hi username! You've successfully authenticated, but GitHub does not provide shell access.
Set your git credentials globally (if not already configured):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"When cloning repositories or setting remotes, use SSH URLs instead of HTTPS:
# SSH format (recommended)
git clone git@github.com:username/repository.git
# HTTPS format (not recommended for authenticated operations)
git clone https://github.com/username/repository.gitTo change an existing repository from HTTPS to SSH:
git remote set-url origin git@github.com:username/repository.gitIf you see this error, ensure:
- Your SSH key is added to GitHub
- You're using the correct SSH key
- The SSH agent is running:
eval "$(ssh-agent -s)" - Your key is added to the agent:
ssh-add ~/.ssh/id_ed25519
Check your internet connection and verify you can reach github.com:
ping github.comIf you have multiple GitHub accounts or SSH keys, configure them in ~/.ssh/config:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_work