Can't push to bitbucket, Permission denied (publickey)

I am trying to push my project onto my bitbucket, been messing with this for about 4 days pouring through countless problem solving/pages/troubleshooting/tutorials. Im at a loss and very frustrated. I have done this before but on different computers...anyway here is the code/response that I'm getting

~/dev/sample_app git push -u origin --all
The authenticity of host 'bitbucket.org (131.103.20.168)' can't be established.
RSA key fingerprint is 81:7b:2c:f5:6f:18:2b:7c:4b:ec:aa:46:46:74:7c:40.
Are you sure you want to continue connecting (yes/no)? 
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
~/dev/sample_app 

I am on a mac running 10.8.4.

So a little progress has been made, initially there was no .ssh folder so I created that way back in the beginning, there was no known_hosts file so I ran

ssh -T [email protected]

I chose yes and this created a known_hosts file and when I tried to push again I got:

~/dev/sample_app git push -u origin --all
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

My .ssh folder is 700 and the keys inside are all 600.


Solution 1:

You can set IdentityFile flag file in ~/.ssh/config file as follows:

Host bitbucket.org
  IdentityFile ~/.ssh/id_rsa

When you run

ssh [email protected]

the ssh client allows you to selects a file from which the identity (private key) for RSA or DSA authentication is read.

SSH Client To Use Given Private Key ( identity file )

Solution 2:

You might be using ssh as the git origin url. Try removing the ssh origin like so

git remote rm origin

Then add new origin with HTTPS url and try pushing again.

git remote add origin https://[email protected]/SOMETHING/SOMETHING.git
git push -u origin master

Make sure you paste your url from bitbucket as origin.

Solution 3:

Edit: As Dan Swain points out in the comments, from 1 March 2022 this answer will have been superseded by authentication policy changes: https://bitbucket.org/blog/deprecating-atlassian-account-password-for-bitbucket-api-and-git-activity

The same applies to Github repositories as well, FWIW.

Thanks for the heads-up, Dan.

It might make sysadmins recoil in horror, but after suffering this problem (Windows) I gave up on SSH and went back to HTTPS.

When first adding the remote repository to Git, replace the SSH reference '[email protected]...' with the HTTPS URL 'https://<username>@bitbucket.org'.

You have to type your password in every time but, particularly under Windows where SSH is not as commonly available as with the *nix family, I see this as a minor inconvenience compared with the headaches of SSH.