Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,863
Community Members
 
Community Events
185
Community Groups

Permission denied (publickey) after restart

I'm using SourceTree 2.4.1 on Mac with my Bitbucket account. I authenticate with Google and use SSH.

Every time I have restarted my system, I'm getting an error in SourceTree (see below).

I then remove my account in SourceTree and set everything up again (including SSH) to get things working again which is quite frustrating if you need to do it at least once a week.

Can anyone help me out?

-------------------

git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=sourcetree fetch --prune origin

Permission denied (publickey).

fatal: Could not read from remote repository.

 


Please make sure you have the correct access rights

and the repository exists.

Permission denied (publickey).

fatal: Could not read from remote repository.

  

 

 

6 answers

1 accepted

23 votes
Answer accepted

If your config file uses UseKeychain yes and AddKeysToAgent yes for Bitbucket/GitHub and you're finding ssh-add -K only works until you reboot your Mac, make sure the Host is set to github.com and bitbucket.org. This is actually the alias for the host record. SourceTree was causing SSH to use the default id_rsa key because it wasn't using the alias name when firing the ssh command.

This fixed the recurring issue for me that forced the awful GitHub Desktop upon me. Thank goodness.

Thank you for this answer.

I had the same issue and in fact the ssh config file, generated by Sourcetree (v2.6.3) was including this info :

# --- Sourcetree Generated ---
Host BunCBG-Bitbucket
    HostName bitbucket.org
    User bunCBG
    PreferredAuthentications publickey
    IdentityFile /Users/christophe/.ssh/bunCBG-Bitbucket
    UseKeychain yes
    AddKeysToAgent yes
# ----------------------------

You'll notice that the host is in fact the name of my key. I just changed it to bitbucket.org and it seems the problem is gone. My SSH key is now correctly detected.

Like # people like this

Thank you, this fixed my issue.

This works until you have only one Bitbucket account.

In my case, I've two different accounts - a private and one for ma company.
Of course the host is the same both times (bitbucket.org). Means it will always use the first entry and uses the wrong key, if I want to authenticate with the key of the second entry.

I thought the user field has effect for the request, but if not even the host matches, the user doesn't matter.

I can set the host to user@bitbucket.org, then I've to change the remote address of the repository, which works quite well. The problem then is, that submodules always use git@bitbucket.org, otherwise my colleagues have the wrong username inside their .gitmodules file.

Is there any solution for different Bitbucket accounts? Besides, why SourceTree writes "Host User-Bitbucket" into the ssh config file, if it doesn't work?

Deleted user May 21, 2018

Thanks Christophe!!!! 
I was going crazy!

Thanks Christophe!!!! 

Christophe SAUVEUR: "You'll notice that the host is in fact the name of my key. I just changed it to bitbucket.org and it seems the problem is gone. My SSH key is now correctly detected." 

Thanks! It works for me

Worked perfectly for me on MacOS Mojave 10.14.3!

Thanks mate. It fixed my issue.

Thank you! Fixed my issue as well.

Joshua Alzate
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
Apr 26, 2023

Thanks! I get frustrated every time my machine is rebooted and I have to setup my git credentials again and again.

I've recently discovered something really useful with this issue.

The core.sshCommand config for git. It allows to select the specific SSH key per repository.

More information here: https://superuser.com/a/912281

I am using SourceTree. Where would I add the code they mention there?

git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"

As you are using macOS, you can do Actions > Open in terminal

Howeever, I suggest adding the --local attribute to the command-line if you want to specify core.sshCommand only for the current repository.

Thanks for the feedback. I have solved all my problems by switching to Geico (...err, I mean to HTTPS).

This is not working for me. When I reboot my machine, I have to add my key to the agent.

I have followed the answer given by @Christophe SAUVEUR on January 13, 2018 and entered the required info in the ssh config file. Yet after reboot, the key is lost and have to re-add it with `ssh-add -K ...`

Using macOS Mojave 10.14.2. Any ideas?

Thanks.

You're using two accounts of bitbucket I guess? Otherwise the answer you mentioned is working. Maybe delete all the passphrases from your keychain app and try to add the keys per hand afterwards (see commands below).

My workaround is to comment out all the sections where the "Host" is exactly the same which I want to use now, except the latter one.
Then delete and re-add the keys (ssh-add -D and ssh-add -A)

Thanks for the reply, but it doesn't work. I only have a single account.

Before, my config file was:

Host *
UseKeychain yes

Then I tried to add:

Host *
UseKeychain yes
AddKeysToAgent yes

None of these worked. I still had to use `ssh-add -K ...` each time I rebooted. I Googled this issue a few times and read various answers on stackoverflow. They all recommended the above solutions.

Then I found this thread recently and changed my config file to this (as per above recommendation) - where the <...> entries represent my actual user name:

Host <user>-Bitbucket
HostName bitbucket.org
User <user>
PreferredAuthentications publickey
IdentityFile /Users/<USERNAME>/.ssh/<my-ssh-keyfile-name>
UseKeychain yes
AddKeysToAgent yes

I have already did `ssh-add -D` and re-added the keys before with `ssh-add -K` and also tried `ssh-add -A`. But after restart, I still have to add them again because they are not remembered.

Totally frustrating.

Older question but still an issue I just encountered. This answer is working for me for multiple accounts on mac osx (sierra):

https://apple.stackexchange.com/questions/48502/how-can-i-permanently-add-my-ssh-private-key-to-keychain-so-it-is-automatically

0 votes
jyo
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 25, 2017

Hi Maël,

If you're on 10.12, I'd recommend entering a few values into your SSH config, which should autoload your keyfile: https://developer.apple.com/library/content/technotes/tn2449/_index.html

Cheers,
Jesse

Hi Maël,

do you have any solution for my problem I reported above? I've commented to Ben Stones answer.

Would be nice, because I've the problem since a year and it's really annoying - not only my problem, also to change every time the Host field of the ssh config.

BG
Andreas

Hi Andreas,

Since I have only 1 account Ben's solution worked for me.

I don't know how to fix this for multiple accounts, sorry!

- Maël

Oh sorry, the question was actually meant for Jesse.

Thank you anyway :)

I have just tried re-connecting with Google (instead of removing the account) and it worked.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events