Having Trouble Creating SSH Key And Connecting To GitHub On Mac
Asked by djooryab:

Hello,

I've been struggling to create an SSH key in SourceTree on Mac, and have it successfully connect to my GitHub repo. I try to add my account and click on the "Generate SSH Key" option, and that works fine. But, then it gives me an error saying that the SSH key failed to upload to GitHub and that I should manually add the SSH Key. Ok... So I do that and copy and paste the public key from Sourcetree into GitHub, but then, when I try to clone my repo, I get infinitely stuck on the spinning wheel while my remote is loading. How can I set up SSH keys with Sourcetree and Github on Mac?


Answers:
author: djooryab
body: Hello everybody,

I have great news. I found a good solution to this problem which I will outline for everybody below. This solution works for authenticating using SSH AND 2-Factor Authentication for SourceTree 4.0.2 on Mac. Now you can have security of SSH and 2-Factor Authentication, and keep using your favorite Git GUI yay!!.

Steps:

1. Open the app called "Keychain Access" on Mac.

2. Search for "git" and delete all the keys you find (assuming you aren't using those keys for any other things)

3. Go to your home folder

```
cd ~
```

4. Delete your .ssh folder (ASSUMING YOU AREN'T USING ANY KEYS IN THERE!!)

```
rm -rf .ssh
```

5. Create a new SSH Key

    ssh-keygen -t ed25519 -C "your_email@example.com"

6. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.

7. At the prompt, type a secure passphrase.

    > Enter passphrase (empty for no passphrase): [Type a passphrase]> Enter same passphrase again: [Type passphrase again]

8. Start the ssh-agent in the background.

    $ eval "$(ssh-agent -s)"> Agent pid 59566

9. Check to see if your `~/.ssh/config` file exists in the default location.

    $ open ~/.ssh/config> The file /Users/you/.ssh/config does not exist.

10. If the file doesn't exist, create the file.

    $ touch ~/.ssh/config

11. Open your `~/.ssh/config` file, then modify the file, replacing `~/.ssh/id_ed25519` if you are not using the default location and name for your `id_ed25519` key.

    Host *  AddKeysToAgent yes  UseKeychain yes  IdentityFile ~/.ssh/id_ed25519

**Note:** If you chose not to add a passphrase to your key, you should omit the `UseKeychain` line.

12. Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace *id_ed25519* in the command with the name of your private key file.

    $ ssh-add -K ~/.ssh/id_ed25519

13. Add the SSH key to your GitHub account.

14. Go to an empty folder and clone your GitHub repo

```
git clone git@github.com:your_email@example.com:your_username/ProjectName.git
```

15.

```
Cloning into 'YourProject'...

The authenticity of host 'github.com (192.30.255.112)' can't be established.

RSA key fingerprint is SHA256:aaaaabbbbbcccccddddeeee.

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
```

16. Open SourceTree and add your account with Auth Type: OAuth, Protocol SSH, then click on "Connect Account". DON'T click on Generate Key, leave that blank.

17. Once you successfully connect, you will see an orange warning icon next to the SSH text. Ignore that, it means nothing.

18. SourceTree will work now with your remotes!!!

comments: author: Eric
body: Thanks! This fixed the issue for me. Step 14 \& 15 was really key to make this work for me. I initially tried cloning the repo through SourceTree but that didn't work. I guess it really needed the known hosts-file that would only get created by cloning via command line.


author: Andreas Lee-Norman
body: Amazing! thank you. I've been fighting this forever. I can't believe that Sourcetree never can get this right


author: timklimowicz
body: This worked for me as well. Like Eric, steps 14 and 15 are what did it for me. I tried many other things previously (including every other step in this list), and nothing seemed to do the trick until I did those things. Thanks!


author: Deyan Yosifov
body: Wow, it is 2024 and SourceTree for Mac still does not setup Github ssh connection out of the box, so your answer helped me a lot, thank you! By the way instead of steps 14 and 15 I used the following command to first test the Github authentication and then I cloned the repo from the SourceTree GUI instead of the terminal:

*ssh -T git@github.com*

The first time this is called you are asked to add the connection to the list of known hosts:

*The authenticity of host 'github.com (140.82.121.4)' can't be established.*

*ED25519 key fingerprint is ....*

*This key is not known by any other names.*

*Are you sure you want to continue connecting (yes/no/\[fingerprint\])? yes*

*Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.*   

After this step I was able to clone my repo successfully from the SourceTree for Mac GUI. Thank you again!


author: Kai Scheffer
body: you may check the permissions on the \~/.ssh/config file, it needs to be 600.

```
chmod 600 ~/.ssh/config
```


author: rj
body: I'm stuck on this issue also, but the steps above don't work for me. No matter what I do I get the "must add SSH key manually" error when adding a user account, even after I just added the SSH key I'm using to GitHub. :( been stuck on this for week+ now...

comments: author: Tom
body: Did you ever find another answer. I also tried the above approach and it did not solve my problem. When I try the following it fails:

ssh-add -l -E SHA256

The agent has no identities.


author: Kevin Ewoldt
body: I had same problem. What finally worked was in SourceTree, opening any repo and clicking the `Terminal` button top right. That opened a terminal within its permissions scope. I then did a `git pull` which prompted me to enter my SSH Passphrase. Once I did that....any git pull/push from the SourceTree GUI worked fine.


Tags: github

ssh