Sourcetree Import Ssh Key Mac



  1. Ssh Key Generation
  2. Sourcetree Import Ssh Key Mac Os

For the past year or so, we’ve been using Git as our version control system. My introduction to the GUIs around Git was SourceTree (although I’ve made an effort to learn the commands) but I have also used poshgit and Git Bash. Recently, we’ve started using SSH keys instead of HTTPS and I had to learn how to set up my repositories with SSH. Everywhere and everyone tells you this is straight forward and it is when the critical path works but when something is wrong, it gets more difficult. A lot of unnecessarily complex documents does not help either. So I’m going to details all the steps that I took in the hope that it could helps someone.

Set up additional SSH keys with Sourcetree (on Windows) Use this section to create as many SSH keys as you need when using Sourcing on Windows. Create an SSH key. From Tools, select Create or Import SSH Keys. From the PuTTY Key Generator dialog, click the Generate button. As the SSH key generates, hover your mouse over the blank area in. Get code examples like 'bit.ly/office2019txt' instantly right from your google search results with the Grepper Chrome Extension.

  • About Press Copyright Contact us Creators Advertise Developers Terms Privacy Policy & Safety How YouTube works Test new features Press Copyright Contact us Creators.
  • Ssh user@host git init -bare /path/to/repo.git. Be sure to use a valid SSH username for user, the domain or IP address of your server for host, and the location where you'd like to store your repo for /path/to/repo.git. Note that the.git extension is conventionally appended to the repository name to indicate that it’s a bare repository.
  • Space-lead key bindings are organized in mnemonic namespaces. For instance, buffer actions are under SPC b, file actions are under SPC f, project actions are under SPC p, search actions are under SPC s, and so on. Keybindings are consistent across the whole distribution thanks to a set of conventions.

My setup for this task is Git (you can use the embedded git within SourceTree), SourceTree and BitBucket (previously used Google Drive to host my git repositories).

  • Open SourceTree and click on the Terminal icon (this is Git Bash)
  • Type the following command in
    • ls –all ~/.ssh (this will list any existing ssh keys in C:Users.ssh, this is the default but can be changed when generating the key).
  • Next, generate the key
    • ssh-keygen –t rsa –b 4096 –C
    • It will ask you where you’d like to store the files, I accepted the default but you can specify a directory if you wish.
    • Then enter a passphrase, I would recommend you provide a passphrase from a security standpoint.
    • You should now see this this:
  • There should be two key files id_rsa (private) and id_rsa.pub now created.
  • Still using the terminal (Git Bash) in SourceTree, type:
    • eval $(ssh-agent). There are many ways to start the SSH agent but this is only way it would work for me. It should give you a process id back, something like, Agent pid 1234
  • Finally using this command to add the new key
    • ssh-add ~/.ssh/id_rsa
    • If successful, the output should say that an identity has been created.
    • You should never have to type in the passphrase again.
  • Log into BitBucket
  • Select the icon on the top right of the browser and select Manage Account
  • From the Security menu, select SSH Key then Add Key
  • Add you public key (id_rsa.pub) to the text area and then Add Key again

Note, your public key in this file is in a different format from what BitBucket expects. My recommendation for this scenario is to go to SourceTree – Tools – Create or Import SSH Keys. This starts a Putty Generator that has the ability to load existing keys. The generator will then show the public key in a user friendly format to be copied and used within BitBucket.

In Stage 1, the SSH key was generated and set up for the Git Bash terminal, now we want to take that SSH key and use it within the SourceTree GUI.

  • First step is to go to Tools – Create or Import SSH Key
  • Load your existing private key in.
  • Click on “Save Private Key”. This has to be saved in the Putty .ppk format. I would recommend that you didn’t save this private key to the .ssh folder in case of conflicts between two keys.
  • Next is to launch the SSH agent – Putty comes with SourceTree.
  • Make sure Pagent is running ( little computer with a hat on sitting in your windows tray).
Sourcetree Import Ssh Key Mac
  • Add the key to the SSH agent by right clicking on Putty Pagent and selecting “Add Key”. It is Pagent that stops the user from entering the passphrase all the time by holding key and making it available to SourceTree.
  • A further step is to add the .ppk key to Tools – Options – General – SSH Client Configuration.

That’s it! I was all around the houses trying to fix various errors and configure. Some of the problems I faced were:

  • Permission denied (public key). I believe it was a combination of errors on my part. One, I had created too many key files in the .ssh directory and it didn’t know what one to choose. Second, I hadn’t set up SourceTree correctly. The SSH key had to be a .ppk key and not the id_rsa key, which I’d generated.
  • Could not open a connection to your authentication agent. I believe this was down to me changing from Putty to OpenSSH. OpenSSH just never launched, no wonder it couldn’t get a connection.
  • It took ages to clone a repository. SourceTree GUI doesn’t give a lot of feedback with what is going on, not like Git Bash. I thought it wasn’t working.

My tip would be to test the connection using “ssh –T git@bitbucket.org”. This command with provide decent feedback if you have or haven’t authenticated. So open Git Bash and type this in.

A good topic for debate is why go to all the trouble of using SSH keys? Why not, use HTTPS and cache you account details in winstore?

Update:

Discovered this morning that if you shut SourceTree down, if you use the Git Bash terminal, you will need to repeat Stage 2.

References

  • September 12, 2015 . Coding . Comments
    Tags: SSHGit

Ssh Key Generation

We use Git and Sourcetree, primarily on the Windows operating system and love it 99% of the time. Sometimes we work from our personal computers and being developers, we have our own projects outside of work that are associated with different credentials. To avoid SSH key issues we just use simple HTTPS to clone our repos down instead of hassling with multiple SSH keys.

This week we have officially adopted two-factor authentication for our revision control so guess what?!

As the documentation clearly states, this breaks HTTPS login for Git!

All things considered this is a small issue but getting multi-key SSH configured was surprisingly difficult. What I found most interesting was that every document explaining how to do this omitted one or two critical details. I have recorded the steps I took to guarantee a sure-fire process to achieve functional Windows Sourcetree with Git working on multiple SSH keys.

Obviously this is Windows and Git specific. We'll be implementing the scenario where you have 1 work and 1 personal Github/Bitbucket/Gitlab account

  1. Configure Sourcetree to use OpenSSH. We don't use HG so no issues for us!
    • Existing Keys: If you previously used Putty, load your ppk into PuttyGen and convert it to Open SSH format using the conversion option. Append .rsa to the file name so you can differentiate from your ppk format private key.
    • Fresh Start: If you want to start from scratch, generate two new keys in PuttyGen. 1 for work and 1 for home. Save the private and public keys for later use.
  2. Check that both of your personal and work account have the correct public keys added for access.
  3. Create a .bashrc file at %userprofile%.bashrc and save the following:
    #! /bin/bash
    eval `ssh-agent -s`
    # Note you make change the .rsa to match whatever suffix you choose for your OpenSSH private keys.
    ssh-add ~/.ssh/*.rsa
  4. <a' name='step4'></a>Create a %userprofile%.sshconfig
    Host work
    HostName host.org
    # The ~/ means your profile directory. work.rsa should be changed to match
    # the OpenSSH private key name for your work account.
    IdentityFile ~/.ssh/work.rsa
    # Tell SSH to only use identities used in this file
    IdentitiesOnly yes
    Host home
    HostName host.org
    # home.rsa should be changed to match
    # the OpenSSH private key name for your home account.
    IdentityFile ~/.ssh/home.rsa
    IdentitiesOnly yes

    See the SSH Docs for more information about these options

  5. Close Sourcetree
  6. Close all open terminals/shells/cmd prompts
  7. Relaunch Sourcetree
  8. Launch Terminal via Sourcetree</p>

    and verify that you see similar output:

    Agent pid 11740
    Identity added: /c/Users/yourname/.ssh/home.rsa (/c/Users/yourname/.ssh/home.rsa)
    Identity added: /c/Users/yourname/.ssh/work.rsa (/c/Users/yourname/.ssh/work.rsa)
  9. If you do not see that output, your .bashrc file is incorrect or in the wrong directory. Redo Step 3. If that still doesn't work, start over because you probably have the wrong format keys.
  10. Running ssh-add -l should list the thumbprints for your two SSH keys.</p>
  11. You are now ready to clone/pull/push some code!
  12. From here, depending on what order you loaded your keys either your home or work will be treated as default. For me, my home registers as default because either key name or thumbprint is alphabetically first. No matter though; we can fix this!

    </li></p>
  13. Repos belonging to the non-default account will need to have their git origins modified by replacing the hostname portion of the url with the alias id from your .sshconfig file.</p>

    In my case I would replace my origin git@github.com:myname/repo.git with git@work:myname/repo.git.

  14. Once you have updated your git origin for the secondary account to use the config file's alias from step 4, you should be able to push/pul without issue.
    • If you do have an issue, use Sourcetree menu Tools -> Add SSH Key... dialog to try re-adding the key.
    • If that doesn't work, close Sourcetree, any open console, and try again!
    • If that doesn't work then you probably missed a step or detail. Try again!

  15. One final tip before you go. Be sure to clear out any saved username/passwords from the Sourcetree authentication tab that are related to your work/home accounts. If you don't remove these, Sourcetree will keep alerting you to that fact the HTTPS login cannot be performed via OpenSSH. Not a big deal but the modal dialogue gets lost and is super annoying to find.

  16. </li>

Sourcetree Import Ssh Key Mac Os

Good luck and happy coding!

Comments Section

Feel free to comment on the post but keep it clean and on topic.

Please enable JavaScript to view the comments powered by Disqus.blog comments powered by Disqus