Configure GitHub or Bitbucket using SSH connection with visual studio or command prompt
Overview:
You must be aware that you can connect to remote repo (GitHub, BitBucket, GitLab, Assembla, etc. the GIT provider) using HTTPS or SSH. There is a lot of article debating about which one to use, I follow a couple of simple rules:
a) SSH is preferred if you are looking for better security. HTTPS communication is authenticated using username and password and we all know the vulnerability of only password-based authentication.
Please note GitHub is moving to token-based authentication from Aug 2021. (https://github.blog/2020-12-15-token-authentication-requirements-for-git-operations/)
b) SSH is one-time set-up activity and does not get disrupted if you wish to change your password for any GIT provider. The best part is you can use the same SSH key for all the GIT providers. This is very convenient if you are working with various GIT providers based on your different client requests.
You can further read here: https://ourtechroom.com/tech/https-vs-ssh-in-git/
SSH pre-requisites:
Before we get into the detailed steps, we must know few fundamental concepts about SSH and how it allows encrypted connection between HOST (Remote server/GIT Remote repo server) and CLIENT (your local system).
You can read the following article for in-depth knowledge. https://www.hostinger.in/tutorials/ssh-tutorial-how-does-ssh-work
Fundamentally, it’s a secure encrypted connection between two systems. You need an SSH client installed in your system (PuTTY, Windows 10 has a built-in SSH client) and the remote server should accept SSH requests.
Steps to connect/clone repo using SSH with GitHub/Bitbucket:
- Let’s open your GitHub/BitBucket account and open the account settings and click on SSH Keys:
2. Now, before we click on add key, we need to generate the key. Remember that SSH connection to remote repo requires a key for authentication.
2.a. Open any SSH Client (Putty or Command Prompt of Windows 10) — If you want to use PuTTY check-out this link: https://www.ssh.com/ssh/putty/windows/puttygen
So, ssh-keygen will prompt you to give the key name. Usually in the .ssh folder within your user profile folder.
2.b. As the prompt already suggesting a default location and default file name, we will stick to it. We will just press enter without typing any name. We want to keep the default. Please note you can store the key anywhere you want, and you can provide the name of your choice. Here we are trying to keep things simple.
2.c. Press enter, now it will ask for a passphrase. For simplicity, we will skip the passphrase. Please note passphrase is used to make the SSH authentication more secure.
Once you skip both the passphrase prompt by pressing enter you should see something similar:
2.d. Let’s check if ssh-keygen created the key or not. Open your explorer and navigate to C:\Users\<your system user id>\.ssh folder. You should see two files, one with a .pub extension another without any extension. The name of the file should be the same. .pub file is the public key and the other one is the private key.
2.e Now, open the public key file in notepad and copy its entire content. Keep in mind that while copying you are not modifying anything in the file. Also, keep in mind not to copy any additional return key at the end of the content.
3. Now, go back to your BitBucket or GitHub browser window and click on the add-key button and paste the key value. You also need to give a name to the key. The name does not have any significance other than just for your own reference.
4. Now, copy the Clone SSH URL of your repository.
5. Using SSH URL to clone the repo with Visual Studio
5.a. Open visual studio and click on clone repo. You will be prompted with a window like this:
Enter the copied SSH URL from your GitHub/Bitbucket repo. Please remember the path should start with git@. Please note In bitbucket the URL comes with the git clone command which you need to remove. git clone command will be helpful when we will clone the repo using the command line.
Also, choose the local path where you want to clone the repo. Once done click on the clone button.
5.b. Once you click on the clone, you will see a screen to validate the Host Fingerprint. It will prompt you to store the RSA key fingerprint. RSA fingerprint is nothing, but a key supplied by your host to identify if you are connecting to the right host. Read more here. https://superuser.com/questions/421997/what-is-a-ssh-key-fingerprint-and-how-is-it-generated
You will notice that there will be a new file created named known_hosts inside your .ssh folder. And if you open the file, you will see the entry of GitHub.com and its IP address and the RSA fingerprint value. Please note once you add the RSA for one host you will not be prompted next time to save the RSA Key Fingerprint for the same host. In this example, I have already added Github, next time while cloning any repo from GitHub will not prompt me to save the RSA key.
5.c. Once you click on yes to validate the Host Fingerprint, the cloning should start immediately.
5.d. If you have correctly followed the steps, you should see that repository files are being copied into your local system. If you see an error message like “Git failed with a fatal error, Please make sure you have correct access rights”, it is most likely that you have copied the wrong key in GitHub and BitBucket SSH key input.
6. Cloning using command prompt
Remember the steps are all same to configure the SSH what you have done till steps 4. Now, open the command prompt and change the folder location where you want your repositories to get copied.
Then write the git clone command followed by the URL as shown below.
Now, the rest of the actions are the same, as we did for the Visual Studio path. First, it will validate the RSA Fingerprint (if already not done) for a specific host (GitHub, BitBucket, etc), and then it will start cloning the files from the remote server. The error conditions are also the same as the Visual studio path.