48

I have created a repository in BitBucket and have cloned it using SSH. When attempting to push my changes to BitBucket, I receive the following error:

Unauthorized

fatal: Could not read from remote repository.

I have created an SSH key using ssh-keygen and have copied the resulting public key to BitBucket. I have downloaded SourceTree to see if a bit of tooling might help, but it has the same problem.

I've been trying to fix this for hours now and don't want to waste any more time on it. Surprising, I couldn't find anyone with a similar issue or even the same error messages when searching on Google.

Please help! I am running on Mac OS X and have tried deleting GitHub credentials from Keychain Access but this hasn't solved the issue. Does anyone know what I might be doing wrong?

Update

I have used exactly the same client-side configuration with GitHub, and ran into no issues, so I'm suspecting it's some configuration on BitBucket that I'm missing.

serlingpa
  • 12,024
  • 24
  • 80
  • 130
  • Git remote show origin says the same i guess? Perhaps have a look at git config --list -- show-origin... – Christoph Jun 22 '19 at 20:20
  • What am I looking for? I see `remote.origin.url=git@bitbucket.org:meansoft/interoffice.git` which I think is correct – serlingpa Jun 22 '19 at 20:21
  • Is there a credential.helper? https://help.github.com/en/articles/updating-credentials-from-the-osx-keychain or https://www.testingexcellence.com/install-git-mac-generate-ssh-keys/ – Christoph Jun 22 '19 at 20:28
  • Tried all of that @Christoph, but it doesn't work – serlingpa Jun 22 '19 at 21:02
  • Have you verified your ssh settings, see "Step 4'.9 at https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html#SetupanSSHkey-ssh? – joran Jun 23 '19 at 05:39
  • I have, done that too. – serlingpa Jun 23 '19 at 14:28
  • Just to cover all the bases: do you have write permission to that repo? Does `ssh -Tv git@bitbucket.org` return your username? – Jim Redmond Jun 24 '19 at 18:39

4 Answers4

155

Just had the same issue and solved it.

My issue was that I added the generated public ssh key to the repo itself through the 'settings' of that repo.

The solution was to delete the ssh key from the repo itself. Click on your account. Add the ssh key through 'settings' of your account.

Barry D.
  • 1,551
  • 1
  • 5
  • 2
  • After deleting the ssh key from the repository, BitBucket complains with this message and doesn't allow me to add the same key to my profile/bitbucket settings: "Someone has already added that key as an access key to a repository." – Ulysses Alves Nov 02 '19 at 19:50
  • 1
    those settings are hidden under the account profile icon > Personal Settings then Security > SSH keys – jobwat Jun 13 '21 at 21:51
  • So what's the purpose of the repo specific SSH keys then? – Kyle Jun 18 '23 at 06:41
  • Location: Click on Settings Icon (top-right) > Personal Bitbucket settings > SSH keys – rommyarb Jun 23 '23 at 06:58
88

Problem: Your public ssh key is added in REPO -> Settings -> Access keys

It only gives READ-Only Access as stated there

Use access keys to gain read-only access to this repository. Learn more about using SSH keys.

Solution: Add public ssh key in

Your profile -> bitbucket settings -> SSH-Keys

Martijn
  • 15,791
  • 4
  • 36
  • 68
Omer Aslam
  • 4,534
  • 6
  • 25
  • 24
  • 9
    Oh man! I don't know whether to hug you or to punch myself. Hey @serlingpa make this the accepted answer! – Frazer Sep 25 '19 at 21:25
  • 1
    use ssh -T git@bitbucket.org to test your key. In my case, after I remove all repo using same key with my personal profile ssh keys, problem resolved. – Orange Jul 14 '21 at 09:07
  • Why is this answer not being accepted? – Rex Bengil Jan 04 '23 at 08:16
1

I had the same issue and I have slightly different solution:

  1. Created new RSA key pair via ssh-keygen -t rsa use password for it if you want
  2. Then added the SSH Key to the ssh-agent with ssh-add ~/.ssh/<your_private_rsa_key>
  3. Add this rsa key to your bitbucket account Bitbucket -> Your Profile -> SSH Keys
  4. I logged in to the bitbucket with ssh -T git@bitbucket.org

These steps resolved the problem for me.

svetlio
  • 41
  • 4
0

You might check to see if you cloned the repository using HTTPS or SSH. Assuming you're using SSH, you could try explicitely setting up your ssh client configuration in ~/.ssh/config and define the host/private key to use:

Host <bitbucket host>
  IdentityFile <path to ssh private key>
sybase
  • 11
  • 1