* prerequisite: GitHub account,Vscode, and Git CLI
Https is a common and secure way to clone a Git repository. One of it’s drawbacks, it may require your username and password in the url. There are ways around it, so let’s talk about using SSH.
SSH to clone a Git repo is a better option. When it comes to best practices. SSH or (secure shell) provides a secure connection from sever to your local machine using key pairs. The keypair consists of a public and a private key. The public key will be stored to your Git repo while the private key is save to a file on your local machine. This will allow the Git repo and your local machine to communicate using ssh. This limits repo access to the machine that generates the key pair, and to whoever is issued a copy of the private key.
Let’s “Git” started.
First you want to create a new repository. Login into you GitHub Account, select new repository and make it private. Give the repo a name, add .gitignore and README.md, create repository.
*Note: when adding .gitignore select the correct language for the repo, this will insure certain files will not be tracked by GitHub and will not be seen in the repo.
Open up vscode , select the view tab and open a new terminal. In the terminal switch to the Git Bash shell. Using the CLI we need to create a new directory to store the cloned repo. Change into the new directory. Follow the commands and example below.
###create new directory
mkdir <name-of-directory>
###change directory
cd <name-of-directory>
To clone the repo via ssh we will need to create a keypair. ssh_keygen command will generate a public/private rsa key. Using the CLI enter the command below.
ssh-keygen -t rsa -b 4096
After the key pair is generated you will be asked to enter a file to save the key. Note that it creates a default path to where the key is saved if left unchanged. I used the default path for simplicity. Press Enter 3x
*Note: For this example we skipped passphrase, but adding passphrase is a best practice as it adds security to your ssh keys.
Now cd into the directory where the key is saved (in this case .ssh) and use the ls command to list the files in the directory. You will see the id_rsa and id_rsa.pub, Using the cat command open the the public key, and copy the text. This will be used in your GitHub account in the next step. Follow the commands and example below.
###list file in directory
ls
###open public key file
cat id_rsa.pub
Back in GitHub click your profile picture in the top right hand corner, select settings. Under setting select SSH and GPG keys, select ssh and new ssh key. Give the key a name, in the box below key paste in the public key you copied from the step above. click add ssh key.
Go to the repo you wish to clone and select code, copy the ssh link and return to the terminal in vscode.
Using the cd command change to the directory you created to save the cloned repo. Type git clone and paste the link copied from GitHub. When prompted to type yes to continue. Follow the command and example below the repo is now cloned.
git clone <ssh-link-from-GitHub>
Lastly go to file, open file, and select the file with the name of the cloned repo. All of the contents of the repo are ready to use in vscode.
Congats!!! You have cloned your private GitHub repo using ssh and vscode. I hope you find this helpful. Check out my other medium post for more tutorials in tech.
Thanks for your time and attention.