DEV Community

Cover image for Using SSH to connect local project to github
Ibe Arua
Ibe Arua

Posted on

Using SSH to connect local project to github

Github

Up until now, connecting your local project to your remote github repository required only using your github using username/password, but now to sync your project with remote github repo, you would need to connect using ssh.

Git allows you to manage and track changes to your source files. Git is often used in group projects, where a remote git repository such as one hosted on Github, acts as the central point for each team member to submit changes to the source code and view other team members contributions.

The SSH protocol adds a secure layer when using remote git repositories through the use of git SSH keys. Itโ€™s important to have security measures in place when managing access to source code stored on a remote git repository.

When you create a github repository, you are given a local url with which to connect the repo to your local machine for the purpose of syncing the project with your development machine hence allow continuous integrations and delivery. you either clone the repo to your local machine, or using https, ssh or github cli to connect and pull/push to the repo. Up until August, 2021 github GitHub is no longer accepting account passwords when authenticating Git operations. You need to add a ssh public key instead.

My assumption is that you already know how to create a github repository, if you don't, it is matter of creating a github account and locating the new repository button then you are good to go, next will be to clone the repo to your local machine:

Using HTTPS to connect local github repo to remote repo

git remote add origin https://github.com/username/repo-name.git
Enter fullscreen mode Exit fullscreen mode

Using SSH to connect local github repo to remote repo

git remote add origin git@github.com:username/repo-name.git
Enter fullscreen mode Exit fullscreen mode

SSH

SSH allows you to use SSH key to connect your local github repository to github remote repository. SSH which stands for Secure Shell, is a protocol used to open up a secure communication channel between computers.

SSH Key

SSH keys are used for secure communication between your local machine and GitHub. You generate an SSH key pair, consisting of a private key (stored on your machine) and a public key (uploaded to your GitHub account).
To generate a ssh key on your linux machine or any unix based operating system, you can use this command:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode

Above command is also used on windows but you may need to enable the openSSH client and starting the service using this command:

Start-Service ssh-agent
Enter fullscreen mode Exit fullscreen mode

this command will create a cd ~/.ssh directory in the user home directory on your local machine, in it, you would find your private key id_rsa and public key id_rsa.pub. with the keys in your .ssh directory of your home directory, you can use the ssh-add command to add the private key to your ssh-client:

ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

ssh-add is a command-line utility that manages SSH keys by adding them to the authentication agent, which is a program that holds private keys used for public key authentication. This allows you to use your private key to authenticate without entering the passphrase every time.

Connect local Github Repo to remote

Having gotten your public key, you would need to add it to your github repository, follow this link to add the key your repo.

Once the key is added, test the connection with this command:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

If everything goes well, you should be good to go

Happy Coding

Top comments (0)