Home/Error Encyclopedia/Git/permission-denied-publickey
🌿

Git Error

Git Error: Permission denied (publickey)

The remote Git server (GitHub, GitLab, etc.) rejected your SSH key — either none was offered, or the one offered isn't authorized.

What This Error Means

This error means Git attempted to authenticate over SSH and the server rejected the connection because it didn't receive a valid, authorized SSH public key — this is an authentication failure, not a permissions/access-control issue on a specific repository.

Why It Occurs

Most commonly, no SSH key has been generated on the machine yet, the SSH agent isn't running or doesn't have the key loaded, or the public key was never actually added to your account on the Git hosting provider (GitHub/GitLab/Bitbucket).

Symptoms

  • ⚠ Appears immediately on `git clone`, `git pull`, or `git push` when using an SSH URL (git@github.com:...)
  • ⚠ HTTPS-based clone URLs don't trigger this specific error

Common Causes

  • • No SSH key pair exists on the machine yet
  • • The SSH key exists but was never added to the GitHub/GitLab account settings
  • • ssh-agent isn't running or doesn't have the key loaded
  • • Using the wrong SSH key when multiple keys exist for different accounts

How to Fix It

  1. Check if an SSH key already exists: `ls -al ~/.ssh` — look for id_ed25519.pub or id_rsa.pub
  2. If none exists, generate one: `ssh-keygen -t ed25519 -C "your_email@example.com"`
  3. Start the SSH agent and add your key: `eval "$(ssh-agent -s)"` then `ssh-add ~/.ssh/id_ed25519`
  4. Copy the public key (`cat ~/.ssh/id_ed25519.pub`) and add it in your Git provider's account settings under SSH keys
  5. Test the connection directly: `ssh -T git@github.com` — a successful authentication message confirms the key is working
CommandPurpose
ssh-keygen -t ed25519 -C "you@example.com"Generate a new SSH key pair
ssh-add ~/.ssh/id_ed25519Load the key into the SSH agent
ssh -T git@github.comTest whether GitHub accepts your key
Advertisement

Verification

  • ✓ `ssh -T git@github.com` returns a successful authentication message with your username
  • ✓ A subsequent `git clone`/`git push` over SSH completes without the error

Prevention

  • → Add your SSH public key to your Git provider account immediately after generating it, before your first clone attempt
  • → Use `ssh-add -l` to confirm which keys are currently loaded when working across multiple accounts

Related