Swipe left or right to navigate to next or previous post

Access GitHub account using SSH

30 Mar 2022 . category: Git . Comments
#Git #SSH #Server

Ultimate guide to How to access GitHub account using SSH

This blog post is about how to access the GitHub account using the SSH. The tutorial uses the ubuntu server and GitHub. However, steps are pretty much similar with other Operating systems and source code management tool

What is SSH key?

SSH (Secure Shell) is secure cryptographic network protocol which is used to securely connect to the remote server. By using SSH, data can be transmitted to the remote server and download the data from the remote server.

SSH provides the secure encryption and verification to secure communication. It is based on client server model.

Generate the multiple keys

Follow the Generate multiple SSH Keys for different GitHub accounts post to create generate multiple ssh for multiple GitHub accounts

Start SSH Agent using following command

    eval `ssh-agent -s`
    ssh-add

Removes all ssh entries from the ssh-agent

    ssh-add -D

Adds the relevant ssh keys

    ssh-add ~/.ssh/id_rsa_personal
    ssh-add ~/.ssh/id_rsa_office

Update the config file

Append the following content located in ~/.ssh/config file. If the file is not available, create a new file.

    Host github.com-office
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_personal
    
    Host github.com-personal
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_rsa_office

Get the SSH repo url

  1. Login to your GitHub account.
  2. Select the repo you want to use or create new repo
  3. On the right-hand side, you will see the Code button. click the button.
  4. You will see the Clone card, click on SSH tab.
  5. Copy the SSH URL of the repo.

Format the SSH repo URL

You will have the SSH repo url in following format for GitHub repo

    [email protected]:username/awesome-project.git

Update the repo url

Update the github.com with the Host like github.com-office for the repo that belongs to the office repo and github.com-personal for the repo belongs to the personal repo.

Final repo url for personal Repo will be like:

    [email protected]:username/awesome-personal-project.git

Final repo url for Office Repo

    [email protected]:username/awesome-office-project.git

Clone the repo

Clone the personal repo using the following command in the terminal

    git clone  [email protected]:username/awesome-personal-project.git

Clone the office repo using the following command in the terminal

    git clone  [email protected]:username/awesome-office-project.git

Update the remote url for existing project

For the existing project, you can change the remote url using the following command

For the personal repo

    git remote set-url origin [email protected]:username/awesome-personal-project.git

For the office repo

    git remote set-url origin [email protected]:username/awesome-office-project.git

origin is the remote name. We usually have the name of remote name as origin. Remote name could be different from origin. Use the actual remote name if origin name is different from origin.


Tapan B.K. | Full Stack Software Engineer

Tapan B.K. is Full Stack Software Engineer. In his spare time, Tapan likes to watch movies, visit new places.