DEV Community

Cover image for Bye Bye Personal Access Token: Cloning and Pushing to GitHub with SSH
Muiz Oyebowale
Muiz Oyebowale

Posted on • Originally published at muizzyranking.github.io

Bye Bye Personal Access Token: Cloning and Pushing to GitHub with SSH

Have you ever stumbled upon a discovery that felt like finding a hidden treasure? Well, that's precisely how I felt when I recently learned how to use SSH to clone and push to GitHub repositories. As someone relatively new to the world of software engineering, this newfound knowledge brought a wave of excitement.

Not too long ago, I found myself on GitHub's remote shores, wrestling with the process of pushing code using HTTPS and personal access tokens. Copying and pasting tokens every time felt like sending secret messages to a digital fortress. And then, a fellow developer mentioned SSH as a solution. It was as if a secret door to a treasure trove had been revealed. SSH—short for Secure Shell—promised convenience and security, and I was all ears.

Picture this: Instead of juggling personal access tokens like they're keys to a secret garden, you have a single, magic key that unlocks all your GitHub repositories. This key is your SSH key, a digital passport that lets your machine communicate with GitHub without the hassle of tokens. No more copying, pasting, or worrying about token expiration. Just pure, streamlined code pushing bliss.

Steps to Cloning and Pushing to GitHub with SSH

Prerequisites:

  1. A GitHub account.

  2. Git is installed on your machine. You can download it from git-scm.com. Alternatively, you can use gitbash on Windows.

  3. Basic familiarity with the command line.

Step 1: Generate SSH Key

Open the terminal (or gitbash if you are using gitbash) in your computer system.

Use the below command to generate a new public-private key pair. Works on Windows, Mac, and Linux. Replace you@email.com with your GitHub email ID.

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

When asked for the location and file name, hit enter to save to the default location.

When asked for a passphrase, either type a secret passphrase and hit Enter, or to leave it empty hit Enter twice.

This will generate two files; a public key ending with .pub and a private key without any extension. (The private key is secret and should never be shared with anyone. Both keys would be in a directory named .ssh.

Step 1.1 Adding key to ssh-agent (for git bash users)

Open git bash and run this command

eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode

It should show you the process ID

Now, add the SSH key to the SSH agent. Use the command below to add the SSH key to the SSH agent.

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

You will get a message saying identity added. Now proceed to the next step.

Step 2: Add SSH key to GitHub.

The next step is to add the SSH key to Github, to do that, you need to copy the SSH key. To view the key = enter the following command.

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Alternatively, you can copy the key directly into your clipboard using pbcopy < ~/.ssh/id_ed25519.pub (macOS) or clip < ~/.ssh/id_ed25519.pub (Windows).

Now go to this URL https://github.com/settings/keys

Or you can navigate manually by clicking your profile icon on the top right corner and clicking settings in the dropdown.

Click on "SSH and GPG keys on the left panel.

Click on "New SSH"

Give it a title of your choice and leave the key type as "authentication key". Paste the key you copied in the "Key" section.

Click on "Add SSH" key button and voila, you can now clone your repository with SSH.

So, there you have it—the tale of how I bid farewell to personal access tokens and embraced the world of SSH. With its convenience, security, and a touch of digital enchantment, SSH has truly made a difference in my coding adventures. The next time you clone and push to GitHub, think about this journey and let the magic of SSH guide your way.

In the grand tapestry of coding, SSH has woven a golden thread of efficiency, and I'm thrilled to share this discovery with you all. Happy coding, fellow explorers, and may your repositories always be filled with the treasures of innovation!

Top comments (0)