Skip to content

Use ssh to connect git repo on GitHub

homepage-banner

Introduction

Git is a powerful version control system used by developers worldwide. GitHub is a popular web-based platform that allows developers to store, share, and collaborate on Git repositories. One of the primary ways to connect to a GitHub repository is through the HTTPS protocol, but using SSH is much more secure. In this blog post, we will discuss how to use SSH to connect to a Git repository on GitHub.

Generate SSH Key

Before we can use SSH to connect to GitHub, we need to generate an SSH key. To do so, open the terminal and enter the following command:

ssh-keygen -t ed25519 -C "your_email@example.com"

This command generates a new SSH key using the Ed25519 algorithm and the email address associated with your GitHub account. Press enter to accept the default file location and enter a passphrase for added security.

Adding SSH Key to GitHub

Once the SSH key is generated, we need to add it to our GitHub account. Go to your GitHub account settings and select “SSH and GPG keys”. Click on “New SSH Key” and give it a title. Then, copy the contents of the public key file (usually located at ~/.ssh/id_ed25519.pub) and paste it into the “Key” field on GitHub. Click “Add SSH Key” to save.

Updating Git Config

Now that we have our SSH key added to GitHub, we need to update our Git config to use SSH. In the terminal, enter the following command:

git config --global url."git@github.com:".insteadOf "<https://github.com/>"

This command tells Git to use SSH instead of HTTPS when cloning or pushing to a repository on GitHub.

Testing SSH Connection

To test if the SSH connection is working, enter the following command in the terminal:

ssh -T git@github.com

You should see a message that says “Hi {your_username}! You’ve successfully authenticated, but GitHub does not provide shell access.” If you see any errors, double-check that your SSH key is added correctly and that your Git config is updated.

Git Config

git config --global user.email "you@example.com"
git config --global user.name "Your Name"
Feedback







Small world. Big idea!
  • Welcome to visit the knowledge base of SRE and DevOps!
  • License under CC BY-NC 4.0
  • Made with Material for MkDocs and improve writing by generative AI tools
  • Copyright issue feedback me#imzye.com, replace # with @