Skip to main content

Command Palette

Search for a command to run...

Using SSH to Authenticate Your Terminal on GitHub

Published
4 min read
Using SSH to Authenticate Your Terminal on GitHub

Hey there! Ever wanted to use GitHub from your computer's terminal without having to type your password over and over? It's a pain, right? Well, there's a super cool and secure way to do it using something called SSH. Think of it as a special key that tells GitHub, "Hey, it's me!" so you never have to type your password again.

This article will show you how to set up that key. It sounds techy, but we'll walk through it step-by-step.


What is SSH, Anyway?

Imagine you have two special keys: a private key that you keep safe on your computer, and a public key that you give to GitHub. When your computer wants to talk to GitHub, it uses your private key to prove who you are. GitHub checks that key with the public one you gave them, and boom—you're in! It's a secret handshake that's way more secure than using your password.


Step-by-Step: Your Password-Free Pass to GitHub

Step 1: See if You Already Have the Keys

First, let's check your computer to see if you already have these special keys. Open your terminal (that black or white box where you type commands) and type this:

ls -al ~/.ssh

If you see files like id_rsa or id_ed25519 with a .pub at the end (like id_ed25519.pub), you probably have a key already. If you don't, no worries! We'll make a new one.

Step 2: Make Your Own Keys

If you need new keys, use this command. It's the best and most secure way to do it:

ssh-keygen -t ed25519 -C "your_email@example.com"
  • ssh-keygen: This is the command that makes your keys.

  • -t ed25519: This tells it to use a modern, secure type of key.

  • -C "your_email@example.com": Put your GitHub email here. It just helps you remember which key is which.

When it asks where to save the key, just hit Enter to use the default spot.

Then, it will ask for a passphrase. This is like a second password for your key. It's a good idea to create one to keep your key extra safe, but you can leave it blank if you want.

The result should be something like this:

Step 3: Tell Your Computer to Remember the Key

Now, let's tell your computer to remember your new key. This way, you don't have to type your passphrase every time.

First, start the "agent" that holds your keys:

eval "$(ssh-agent -s)"

Then, add your new key to it:

ssh-add ~/.ssh/id_ed25519

If you set a passphrase, you'll need to type it now.

Step 4: Copy the Public Key

This is the key you're giving to GitHub. You need to copy its contents to your clipboard.

  • On a Mac: pbcopy < ~/.ssh/id_ed25519.pub

  • On Windows (using Git Bash): clip > ~/.ssh/id_ed25519.pub

  • On Linux: You might need to install xclip first, then run xclip -selection clipboard < ~/.ssh/id_ed25519.pub.

Step 5: Put the Key on GitHub

  1. Log in to your GitHub account.

  2. Go to your Settings (click your profile picture, then "Settings").

  3. On the left side, click on SSH and GPG keys.

  4. Click the New SSH key button.

  5. Give it a simple name (like "MyPC").

  6. Paste the key you just copied into the box.

  7. Click Add SSH key. You might need to type your GitHub password one last time.

Step 6: Test It Out!

Let's see if it worked. In your terminal, type this:

ssh -T git@github.com

You might see a question asking if you want to continue. Just type yes and hit Enter.

If everything worked, you'll see a message like this:

Hi YOUR_USERNAME! You've successfully authenticated...

That's it! You're good to go.


Connect Local Repository to Remote Repository

Create a new directory on Git Bash and move into the directory:

mkdir <new_dir>

cd <new_dir>

Make the directory a git repository

git init

Check if this was successful

ls -al

You should see the below:

Go to GitHub and create a new repository. Then run the below command in your git Bash terminal:

git config —global url.”git@github.com:”.insteadOf “https://github.com/”

git ls-remote git@github.com:<your_github_username>/<your_repository_name>.git

It should look like the below:

If you get the results under that command, congratulations, you just successfully connected your local git repository to your remote git repository


Don't Mess This Up! Common Mistakes to Avoid

  • Don't Share Your Private Key: The file id_ed25519 is your personal key. Never, ever share it with anyone. Only copy the file with .pub at the end.

  • Remember to Add the Key: If you don't do Step 3, your computer won't remember your key, and you'll have to type your passphrase over and over.

  • Use the Right GitHub Link: Now that you're using SSH, you should use the SSH link for your repos. It looks like this: git@github.com:<your_github_username>/<your_repository_name>.git.

Now you have a much easier way to work with your code on GitHub. Ready to give it a try?


Resources

https://github.com/login

https://www.udemy.com/course/devops-for-beginners-docker-k8s-cloud-cicd-4-projects/learn/lecture/50805795#learning-tools

https://github.com/pravinmishraaws/mini_finance