31
votes

I was able to create a key and connect to github following these instructions via the command prompt successfully:

https://help.github.com/articles/generating-ssh-keys

However, when I try to connect via Sourcetree and putty I cannot. I've tried:

  1. generating a new key with the putty key generator (SSH-2 RSA)
  2. entering a passphrase
  3. saving the private key
  4. saving the public key and adding a .pub extension
  5. copying/pasting the key from the putty key generator window into github
  6. attemping to refresh branches on a pull from my private github repository from my local repo using the SSH clone URL I got from github

I also tried opening the key generated from the github command line instructions and it wanted me to convert it to a putty-type key which I did and saved off, tried with that one. Also no luck.

What am I doing wrong?

6

6 Answers

64
votes

In order to get it worked I ended up going to Tools -> Options -> SSH Client and changing it to OpenSSH. I generated and uploaded several different types of keys trying to get it work as well but I think this is what finally did it.

31
votes

In my case, I needed to switch to a git or ssh based repo path rather than the https based repo path. This causes SourceTree to switch to SSH based authentication.

This setting can be found at Repository => Repository setting => Paths

Example:

Correct repo paths

[email protected]:<username>/<reponame>.git

or

ssh://[email protected]/<username>/<reponame>.git

(Note: if you are working with a repo that isn't yours, replace username with organization name)

Wrong repo path

https://github.com/<username>/<reponame>.git

HTTPS repo paths result in SourceTree trying to be extra smart and failing spectacularly. You get prompted for a username/password GUI dialog which will never work if you have 2 factor authentication enabled.

3
votes

You may want to consider switching from OpenSSH to Putty / Plink and use embedded Git instead of Git provided by host OS. Making ssh-agent work on Windows is a bit more complicated than clicking it out straight from the SourceTree and PuttyGen.

If you want to still use terminal to configure SSH and start ssh-agent please see bottom two steps.

Steps

2
votes

For Sourcetree on MacOS I had to change from OAuth to Basic authentication, use "git" as the username (not my GitHub username), and generate the SSH key and input it into GitHub. Only then could I clone a GitHub repo via SSH in Sourcetree.

1
votes

For Mac versions of SourceTree the Tools menu does not exist.

However, you can add the ssh key to your keychain in Mac OS. See: https://superuser.com/questions/879050/sourcetree-ssh-options-on-os-x

On Mac OSX, the native SSH client can use the built-in keychain directly. To add your private key to the keychain simply use the command:

ssh-add -K /path/of/private/key

As an example if your private key is stored at ~/.ssh and is named id_rsa, you would use the command:

ssh-add -K ~/.ssh/id_rsa

You will then be prompted for your passcode, which will be stored in your keychain. After this you should be ready for a password-less login.

1
votes

To login to Github account using SourceTree you may use access tokens. To create an access token follow these steps.

  1. Go to Settings
  2. Select Developer settings from left pane
  3. Select Personal access token
  4. Click on Generate new token button
  5. Give it a name
  6. Select scopes and generate token (save this token somewhere safe because you won't be able to access this token again)

enter image description here enter image description here

Then in SourceTree app follow these steps (for Mac users, not sure about the other platforms)

  1. Go to Preferences->Accounts
  2. Click add button
  3. Select GitHub from Host dropdown
  4. Choose Basic from Auth Type dropdown and HTTPS from Protocol dropdown
  5. Enter your GitHub username in Username field
  6. Paste the access token generated in the previous process in the Password field
  7. Click save and you're done enter image description here