3. Collaborating on GitHub
SSH Keys & GitHub Authentication Setup
Fix 'Permission denied' push errors forever by generating an Ed25519 SSH key pair, adding it to ssh-agent and GitHub, or authenticating smoothly with GitHub CLI.
- Authenticate cleanly with GitHub CLI (gh auth login)
- Generate an Ed25519 SSH key pair and add the .pub key to GitHub
- Switch remotes between HTTPS and SSH without losing history
One of the most frustrating roadblocks right after your first commits happens when you run git push—and GitHub rejects you with Permission denied (publickey) or refuses your GitHub account password.
GitHub disabled plain password authentication over Git years ago for security. Today, there are two clean, modern ways to prove your identity from the terminal: GitHub CLI (gh) or SSH keys. You only need to pick one and set it up once per computer.
Fastest Setup: GitHub CLI (gh)
If you want the quickest zero-friction setup, install GitHub's official command-line tool (brew install gh on macOS, or winget install GitHub.cli on Windows) and sign in through your browser:
gh auth loginWhen prompted by the interactive menu, choose GitHub.com → HTTPS → Login with a web browser. It gives you a short one-time code, opens your browser to authorize access, and stores a secure credential helper automatically. From that moment on, git push, git pull, and even creating Pull Requests from the terminal work seamlessly.
Permanent Setup: Ed25519 SSH Keys
If you work across servers, prefer not to depend on HTTPS credential managers, or see git@github.com:... URLs at work, SSH keys are the industry standard.
Think of an SSH key pair like a padlock and a physical key:
- Your private key (
~/.ssh/id_ed25519) is the physical key. It stays strictly secret on your own laptop—never email it, screenshot it, or paste it into any website. - Your public key (
~/.ssh/id_ed25519.pub) is the padlock. You can safely hand copies of it to GitHub so GitHub knows how to verify signatures made by your private key.
Run these three commands in order to create a modern Ed25519 key pair (replacing the email with your GitHub email), load it into your background SSH agent, and display the public key so you can copy it:
# Generate a fast, modern Ed25519 key pair (press Enter through prompts to use defaults)
ssh-keygen -t ed25519 -C "you@example.com"
# Start the SSH agent and load your private key into memory
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
# Print the PUBLIC key so you can copy it
cat ~/.ssh/id_ed25519.pubHighlight and copy the entire printed line starting with ssh-ed25519 .... Then open GitHub → Settings → SSH and GPG keys → New SSH key, give it a label like "My MacBook", paste the text, and save.
Finally, verify that GitHub recognizes your laptop:
ssh -T git@github.com
# Expected: Hi username! You've successfully authenticated, but GitHub does not provide shell access.Seeing "does not provide shell access" sounds scary at first, but it actually means complete success—GitHub verified your identity and knows you are allowed to push and pull via Git.
Always copy id_ed25519.pub (ending in .pub) to GitHub—never share or commit id_ed25519. If you ever accidentally leak a private key, delete its corresponding .pub key from GitHub Settings immediately and generate a new pair.
Switching an Existing Repo Between HTTPS and SSH
Cloned a repository earlier using an https:// URL, and now want it to use your new SSH key instead? You do not need to re-clone or lose any local branches. Just inspect your remote and swap its URL:
# Check which protocol the 'origin' remote currently uses
git remote -v
# Update 'origin' to use SSH instead of HTTPS
git remote set-url origin git@github.com:username/repo.gitRun git remote -v one more time to confirm both fetch and push now start with git@github.com:.
Quick Checkpoints
Which SSH key file is safe to paste into your GitHub account settings?