Set Up an SSH Key for SourceTree (Windows)

Purpose

When you set up SSH, you create a key pair that contains a private key (saved to your local computer) and a public key (uploaded to Bitbucket). Bitbucket uses the key pair to authenticate anything the associated account can access. This two-way mechanism prevents man-in-the-middle attacks.

This first key pair is your default SSH identity. If you need more than a default identity, you can set up additional keys.

For security reasons, we recommend that you generate a new SSH key and replace the existing key on your account at least once a year.

You can’t use the same key between accounts. You must create new keys for each individual Bitbucket account.

Set up SSH for Git on Windows

Use this section to create a default identity and SSH key when you’re using Git on Windows. By default, the system adds keys for all identities to the /Users/<username>/.ssh directory.Collapse

Step 1. Set up your default identity

  1. From the command line, enter ssh-keygen.For Windows 7 or earlierYou can only enter ssh-keygen into the Git Bash window. It won’t work in the Command prompt.The command prompts you for a file to save the key in:$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa):
  2. Press enter to accept the default key and path, /c/Users/<username>/.ssh/id_rsa.We recommend you keep the default key name unless you have a reason to change it.To create a key with a name or path other than the default, specify the full path to the key. For example, to create a key called my-new-ssh-key, you would enter the Windows path, shown here:$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa): c:\Users\emmap1\.ssh\my-new-ssh-key
  3. Enter and re-enter a passphrase when prompted.The command creates your default identity with its public and private keys. The whole interaction looks similar to this:$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa):
    Created directory '/c/Users/emmap1/.ssh'.
    Enter passphrase (empty for no passphrase):
    Enter same passphrase again:
    Your identification has been saved in /c/Users/emmap1/.ssh/id_rsa.
    Your public key has been saved in /c/Users/emmap1/.ssh/id_rsa.pub.
    The key fingerprint is: e7:94:d1:a3:02:ee:38:6e:a4:5e:26:a3:a9:f4:95:d4 emmap1@EMMA-PC
  4. List the contents of .ssh to view the key files.You should see something like the following:$ dir .ssh
    id_rsa id_rsa.pub
    The command displays two files, one for the public key (for example id_rsa.pub) and one for the private key (for example, id_rsa).

Step 2. Add the key to the ssh-agent

If you don’t want to type your password each time you use the key, you’ll need to add it to the ssh-agent.

  1. To start the agent, run the following:$ eval $(ssh-agent)
    Agent pid 9700
  2. Enter ssh-add followed by the path to the private key file:$ ssh-add ~/.ssh/<private_key_file>

Step 3. Add the public key to your Bitbucket settings

  1. From Bitbucket, choose Bitbucket settings from your avatar in the lower left.
    The Account settings page opens.
  2. Click SSH keys.
    If you’ve already added keys, you’ll see them on this page.
  3. Open your .ssh/id_rsa.pub file (or whatever you named the public key file) and copy its contents.
    You may see an email address on the last line. It doesn’t matter whether or not you include the email address.
  4. From Bitbucket, click Add key.
  5. Enter a Label for your new key, for example, Default public key.
  6. Paste the copied public key into the SSH Key field.
  7. Click Save.
    Bitbucket sends you an email to confirm the addition of the key.Edit an SSH keyAfter you add a key, you can edit the key’s Label but not the key itself. To change the key’s contents, you need to delete and re-add the key.
  8. Return to the command line and verify your configuration and username by entering the following command:$ ssh -T git@bitbucket.orgThe command message tells you which of your Bitbucket accounts can log in with that key.conq: logged in as emmap1. You can use git or hg to connect to Bitbucket. Shell access is disabled.If you get an error message with Permission denied (publickey), check the Troubleshoot SSH issues page for help.
  9. Open SourceTree and go to Tools > Options. In General tab find “SSH Client Configuration”. Find the path to your private SSH key. For SSH Client choose “OpenSSH”

Now that you’ve got an SSH key set up, use the SSH URL the next time you clone a repository. If you already have a repository that you cloned over HTTPS, change the remote URL for your repository to its SSH URL.

Leave a Reply

Your email address will not be published.