How to solve Permission denied (publickey) error when using Git?


I'm on Mac Snow Leopard and I just installed git.

I just tried

git clone [email protected]:cakebook.git

but that gives me this error:

Initialized empty Git repository in `/Users/username/Documents/cakebook/.git/`
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

What am I missing?
I've also tried doing ssh-keygen with no passphase but still same error.

11/19/2018 9:57:18 AM

Accepted Answer

If the user has not generated a ssh public/private key pair set before

This info is working on theChaw but can be applied to all other git repositories which support SSH pubkey authentications. (See gitolite, gitlab or github for example.)

First start by setting up your own public/private key pair set. This can use either DSA or RSA, so basically any key you setup will work. On most systems you can use ssh-keygen.

  • First you'll want to cd into your .ssh directory. Open up the terminal and run:

    cd ~/.ssh && ssh-keygen

  • Next you need to copy this to your clipboard.
    • On OS X run: cat | pbcopy
    • On Linux run: cat | xclip
    • On Windows (via Cygwin/Git Bash) run: cat | clip
  • Add your key to your account via the website.
  • Finally setup your .gitconfig.
    • git config --global "bob"
    • git config --global [email protected] (don't forget to restart your command line to make sure the config is reloaded)

That's it you should be good to clone and checkout.

Further information can be found at (thanks to @Lee Whitney) -

If the user has generated a ssh public/private key pair set before

  • check which key have been authorized on your github or gitlab account settings
  • determine which corresponding private key must be associated from your local computer

eval $(ssh-agent -s)

  • define where the keys are located

ssh-add ~/.ssh/id_rsa

2/8/2020 11:43:40 AM

More extensive troubleshooting and even automated fixing can be done with:

ssh -vT [email protected]



This error can happen when you are accessing the SSH URL (Read/Write) instead of Git Read-Only URL but you have no write access to that repo.

Sometimes you just want to clone your own repo, e.g. deploy to a server. In this case you actually only need READ-ONLY access. But since that's your own repo, GitHub may display SSH URL if that's your preference. In this situation, if your remote host's public key is not in your GitHub SSH Keys, your access will be denied, which is expected to happen.

An equivalent case is when you try cloning someone else's repo to which you have no write access with SSH URL.

In a word, if your intent is to clone-only a repo, use HTTPS URL ({user_name}/{project_name}.git) instead of SSH URL ([email protected]:{user_name}/{project_name}.git), which avoids (unnecessary) public key validation.

Update: GitHub is displaying HTTPS as the default protocol now and this move can probably reduce possible misuse of SSH URLs.


The github help link helped me sort out this problem. Looks like the ssh key was not added to the ssh-agent. This is what i ended up doing.

Command 1:

Ensure ssh-agent is enabled. The command starts the ssh-agent in the background:

eval "$(ssh-agent -s)"

Command 2:

Add your SSH key to the ssh-agent:

ssh-add ~/.ssh/id_rsa

Got same error report.

Fixed with using HTTP instead. Since I don't want set "SSH keys" for a test PC.

Change URL to HTTP when clone:

git clone

My problem is a little bit different: I have URL set when adding a existing local repo to remote, by using:

git remote add origin ssh://

To fix it, reset URL to HTTP:

git remote set-url origin

BTW, you may check your URL using command:

git remote -v
origin (fetch)
origin (push)

Hope this will help some one like me. :D


I was struggling with same problem that's what i did and i was able clone the repo. I followed these procedure for iMac.

First Step : Checking if we already have the public SSH key.

  1. Open Terminal.
  2. Enter ls -al ~/.ssh to see if existing SSH keys are present:

Check the directory listing to see if you already have a public SSH key.Default public are one of the following,,,

If you don't find then go to step 2 otherwise follow step 3

Step 2 : Generating public SSH key

  1. Open Terminal.
  2. Enter followong command with you valid email address that you use for github ssh-keygen -t rsa -b 4096 -C "[email protected]"
  3. You will see following in terminal Generating public/private rsa key pair. When it prompts to"Enter a file in which to save the key," press Enter. This accepts the default file location. When it prompts to Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter] Just press enter again. At the prompt, type a secure passphrase.
  4. Enter passphrase (empty for no passphrase): [Type a passphrase] press enter if you don't want to Enter same passphrase again: [Type passphrase again] press enter again

This will generate

Step 3: Adding your SSH key to the ssh-agent

  1. Interminal type eval "$(ssh-agent -s)"
  2. Add your SSH key to the ssh-agent. If you are using an existing SSH key rather than generating a new SSH key, you'll need to replace id_rsa in the command with the name of your existing private key file. Enter this command $ ssh-add -K ~/.ssh/id_rsa

Now copy the SSH key and also add it to you github account

  1. In terminal enter this command with your ssh file name pbcopy < ~/.ssh/ This will copy the file to your clipboard Now open you github account Go to Settings > SSH and GPG keys > New SSH key Enter title and paste the key from clipboard and save it. Voila you're done.