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.




4 answers

1 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 and 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
    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 and it seems the problem is gone. My SSH key is now correctly detected.

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 ( 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, then I've to change the remote address of the repository, which works quite well. The problem then is, that submodules always use, 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?

Thanks Christophe!!!! 
I was going crazy!

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

0 vote
Jesse Yowell Atlassian Team 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:


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.


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 :)

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published May 30, 2018 in Sourcetree

Tip from the team: configuring Git or Mercurial in Sourcetree

Supported Platforms macOS Windows To make using Sourcetree as simple yet powerful as possible we embed (bundle) dependencies such as Git, Git LFS, and Mercurial. We strive to keep these...

556 views 1 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you