Seamlessly use Github using SSH keys

Seamlessly use Github using SSH keys
Photo by Roman Synkevych / Unsplash

GitHub, along with many other code versioning platforms (such as Bitbucket), provides a secure and efficient way to manage and collaborate on projects. By utilizing SSH keys, you can seamlessly interact with your repositories, enabling secure authentication without the need for passwords.

By leveraging SSH's config file, you can simplify repository interaction without having to specify the SSH URL each time. This comprehensive guide will walk you through setting up SSH configurations to streamline your GitHub experience. After completing this guide, you will never have to struggle with your repository access or compromising security.


Step 1: Generating SSH Key Pair

  1. Open your terminal.
  2. Generate an SSH key pair using the ssh-keygen command:
ssh-keygen -t rsa -b 4096 -C "[email protected]"

Replace [email protected] with your email address.

💡
If you have an existing SSH key pair located in ~/.ssh/, you can skip this step. This key pair will be used next in the guide.

Step 2: Adding SSH Key to GitHub

  1. Display the public key using cat ~/.ssh/id_rsa.pub.
  2. Copy the entire content of the public key.
  3. Open your GitHub account in a web browser.
  4. Navigate to Settings > SSH and GPG keys > New SSH key.
  5. Paste the copied public key into the "Key" field and provide a descriptive title.
  6. Click Add SSH key to save it.
Example of an added SSH key

Step 3: Configuring SSH for GitHub

  1. Open or create the SSH config file:
nano ~/.ssh/config

If the file doesn't exist, create it (nano or vim creates it automatically on write).

  1. Add GitHub configurations:
Host github.com
    HostName github.com
    IdentityFile ~/.ssh/id_rsa

Replace ~/.ssh/id_rsa with the path of your private key file

🤔
You can read this article if you'd like to learn more about SSH configurations. It's a very quick concept to grasp and will help you in other ways, such as remoting between different servers!

Step 4: Testing the Connection

Verify the SSH connection to GitHub:

ssh -T [email protected]

You should receive a success message indicating a successful SSH connection to GitHub.


That's it!

You can now use the SSH option to clone and work with your Github repository.

Make your existing local repositories compatible

Normally, you could simply push any changes and reclone, but you can also manually tell git to change the origin URL.

Let's use my Translate Tool project as an example and see how to change that.

 https://github.com/Uptaker/translate-tool.git

HTTPS URL

[email protected]:Uptaker/translate-tool.git

Git URL

  1. Navigate to your repository.
  2. Set the remote URL to use SSH:
git remote set-url origin [email protected]:Uptaker/translate-tool.git

Replace Uptaker/translate-tool.git with your actual GitHub repository URL.

  1. You can now interact with your repository normally as you did before.

Conclusion

By configuring SSH and utilizing the SSH config file, you've streamlined the process of interacting with GitHub repositories. This setup enables seamless and secure authentication, allowing you to manage your repositories efficiently.

Hope this article helped, and that you've learned something new!


Learn more

We've used the SSH config to configure this automatically. Learn its other uses as well to become more productive and secure in this short walkthrough.

Using SSH keys efficiently - intro to the easy to use SSH config
SSH (Secure Shell) is a widely used protocol for secure remote access to systems and secure data communication. It allows users to connect securely to remote servers and services, like GitHub, using cryptographic keys. When managing multiple remote servers or repositories, each requiring a unique SSH key, configuring SSH to
Understanding private and public keys in SSH, and other technologies
When you use SSH to log into another remote computer, the private and public key system is fundamental for authentication. Here’s a quick breakdown of how it works. Public key authentication involves proving to a server that a trusted party’s key matches your private key. These keys, created through complex