🔧 Error Fixes

Git: Permission Denied (publickey) — How to Fix SSH Access


git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

GitHub rejected your SSH connection because it can’t verify your identity. Here’s how to fix it.

Quick Diagnosis

# Test your SSH connection to GitHub
ssh -T git@github.com

If you see “Permission denied,” your SSH key isn’t set up correctly.

Fix 1: Generate an SSH Key (If You Don’t Have One)

# Check for existing keys
ls -la ~/.ssh/

# If no id_ed25519 or id_rsa files exist, generate one:
ssh-keygen -t ed25519 -C "your@email.com"

# Press Enter for default location, set a passphrase (or leave empty)

Fix 2: Add the Key to ssh-agent

# Start ssh-agent
eval "$(ssh-agent -s)"

# Add your key
ssh-add ~/.ssh/id_ed25519

# Mac: add to Keychain so you don't have to do this every time
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Mac users: Add this to ~/.ssh/config:

Host github.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

Fix 3: Add the Key to GitHub

# Copy your PUBLIC key (not the private one!)
cat ~/.ssh/id_ed25519.pub

# Mac shortcut:
pbcopy < ~/.ssh/id_ed25519.pub

Then:

  1. Go to GitHub → Settings → SSH and GPG keys
  2. Click “New SSH key”
  3. Paste the key
  4. Save

Test it:

ssh -T git@github.com
# Should say: "Hi username! You've successfully authenticated"

Fix 4: You’re Using HTTPS Instead of SSH

Check your remote URL:

git remote -v

If it shows https://github.com/..., switch to SSH:

git remote set-url origin git@github.com:username/repo.git

Fix 5: Wrong Key for the Account

If you have multiple GitHub accounts (personal + work):

# Check which key GitHub sees
ssh -vT git@github.com 2>&1 | grep "Offering"

Create separate SSH configs:

# ~/.ssh/config
Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_personal

Host github-work
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_work

Then use github-work in your remote URL:

git remote set-url origin git@github-work:company/repo.git

Fix 6: Key Permissions Are Wrong

SSH refuses keys with loose permissions:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

Complete Setup From Scratch

If nothing works, start fresh:

# 1. Generate key
ssh-keygen -t ed25519 -C "your@email.com"

# 2. Start agent and add key
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

# 3. Copy public key
cat ~/.ssh/id_ed25519.pub

# 4. Add to GitHub (Settings → SSH keys)

# 5. Test
ssh -T git@github.com

# 6. Switch repo to SSH
git remote set-url origin git@github.com:user/repo.git

# 7. Push
git push