Thursday, March 25, 2021

fun with ssh keys bitbucket, sourcetree, and vs code

# Configure SourceTree to use an SSH key and Clone a repo

On a Windows system...

1 Generate an SSH key

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\you\.ssh, this is the 
default but can be changed when generating the key - but don't).

Next, generate the key

$ ssh-keygen –t rsa –b 4096 –C ""

It will ask you where you’d like to store the files, accept the default - but you can 
specify a directory if you wish.  Then enter a passphrase if you so choose.

You should now see this this:

Your identification has been saved in /Users/you/.ssh/id_rsa.
# Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db you@hell.com
There should be two key files id_rsa (private) and id_rsa.pub now created.

or

Logon to a Linux box where you do all your work and scp your keys it to your Windows profile.

2 SSH-agent
Still using the terminal (Git-Bash) in SourceTree, type:

$ eval `ssh-agent -s`

There are many ways to start the SSH agent; it should give you a process id back, e.g.: Agent pid 1234
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.  If you choose to use a passphrase.

3. Add the SSH key to your BitBucket account

Log into BitBucket (https://bitbucket.hell.com)

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 your public key (id_rsa.pub) to the text area and then Add Key again.
The key takes a specific format; it is best to copy it thus:

In Git Bash:

$ cd ~/.ssh
$ clip < id_rsa.pub

In BitBucket, paste key.

4 SourceTree

In 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.

The first step is to go to Tools – Create or Import SSH Key

Load your existing private key.

Click on “Save Private Key”.  This has to be saved in the Putty .ppk format. Save it or not in ~/.ssh .
Next, launch the SSH agent – Putty comes with SourceTree.
Make sure Putty Pagent is running (little computer with a jaunry hat sitting in your Windows tray).

Add the key to the SSH agent by right clicking on Putty Pagent and selecting “Add Key”. 
Note: Pagent relieves the user from entering the passphrase by holding the key and making it 
vailable to SourceTree.

A further step is to add the .ppk key to Tools – Options – General – SSH Client Configuration.

5.  Clone the repo

In main screen you should see the options:
Local, Remote, Clone, &c.

Select Clone.

Under Source/Path URL, fill in:

ssh://you@bitbucket.hell.com/yourproject/yourrepo

(spindled from https://deedoubleewedee.wordpress.com/2015/06/05/setting-up-ssh-keys-for-a-git-repository-using-sourcetree-and-bitbucket/)

Voila!

# Oh hey, VS Code is easy

5. Visual Studio Code

Nothing special is needed after creation of keys.

Clone repository by selecting "Clone" on the mainpage.

However, since PRIV has a few repositories, it would be advised to to set up a:
Multi-root workspace
vid.: http://code.visualstudio.com/docs/editor/multi-root-workspaces

Be sure to save workspace configuration as a file; that way when VS Code is closed, 
all the local git repos will be available.
But wait... there's more!
Say you want to download a single file from your repo and you really do not want
to clone the whole repo?  Here's how to do it with git commands:

In yourrepo you have a file called youare in the directory happy .  You wish
to place the file youare in a directory called somewhere on your client system.


example:

linux
git archive --remote=ssh://you@bitbucket.hell.com/yourproject/yourrepo.git HEAD:happy youare 
| tar -x -C /home/you/somewhere

windows via git-bash
git archive --remote=ssh://you@bitbucket.hell.com/yourproject/yourrepo.git HEAD:happy youare 
| tar -x -C ~/somewhere

windows via git-bash
git archive --remote=ssh://you@bitbucket.hell.com/yourproject/yourrepo.git HEAD:happy youare 
| tar -x -C /c/Users/YOURLANID/testorama


explanation:
git archive
--remote=bitbucketserver
/project/repo.git
HEAD:file/path/in/repo
fileinrepo
| tar -x
-C /existingdirectory/on/server/where/you/are/dropping/file

No comments: