How to Authenticate Your Git to GitHub with SSH Keysโ€‚by@thevirtualbuddy

How to Authenticate Your Git to GitHub with SSH Keys

You're facing an authentication issue while trying to push your project from your machine to GitHub as below. I assume you have done the initial setup of your Git and GitHub. But you're facing a authentication issue. Check for existing SSH keys before you proceed to create an SSH key, quickly check if you have generated any keys previously. Add your public SSH key to your GitHub account. Use the command below to generate a new set of keys, by substituting *[[email protected]] with your GitHub email address.
image
Sonu HackerNoon profile picture

Sonu

Learning, exploring and enjoying cheese ๐Ÿง€.

github social icontwitter social iconinstagram social iconlinkedin social icon

I assume you have done the initial setup of your Git and GitHub. But you're facing an authentication issue while trying to push your project from your machine to GitHub as below.


Git failed with a fatal error.

incorrect_client_credentials: The client_id and/or client_secret passed are incorrect. [https://docs.github.com/apps/managing-oauth-apps/troubleshooting-oauth-app-access-token-request-errors/#incorrect-client-credentials]


Check for existing SSH keys

Before you proceed to create an SSH key, quickly check if you have generated any keys previously. Open your Git Bash and enter the following command:

$ ls -al ~/.ssh


If you get an output something like this, you've your keys generated already. Please proceed with Adding your SSH key to ssh agent.

$ ls -al ~/.ssh
total 18
drwxr-xr-x 1 your.user.name 1049089    0 Sep  9 12:54 ./
drwxr-xr-x 1 your.user.name 1049089    0 Sep 10 22:49 ../
-rw-r--r-- 1 your.user.name 1049089  411 Sep 12 21:37 id_ed25519
-rw-r--r-- 1 your.user.name 1049089  103 Sep 12 21:37 id_ed25519.pub
-rw-r--r-- 1 your.user.name 1049089 1185 Sep  9 14:41 known_hosts

Generate a new SSH key

After you've checked for existing SSH keys, and if you don't see any output, generate a new SSH key to use for authentication, then add it to the ssh-agent.


Run the command below to generate a new set of keys, by substituting [email protected] with your GitHub email address.:

$ ssh-keygen -t ed25519 -C "[email protected]"


Note: If you are using a legacy system that doesn't support the Ed25519 algorithm, use:

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"


When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location.

Enter a file in which to save the key (/c/Users/your.user.name/.ssh/id_ed25519):[Press enter]


At the prompt, type a secure passphrase or press Enter for no passphrase without writing anything.

Enter passphrase (empty for no passphrase): [Type a passphrase]
Enter same passphrase again: [Type passphrase again]

Add your SSH key to ssh agent

Before adding a new SSH key to the ssh-agent you should ensure that ssh-agent is running. Enter the command below to confirm.

$ eval "$(ssh-agent -s)"
> Agent pid 2030


Then add your SSH private key to the ssh-agent.

$ ssh-add ~/.ssh/id_ed25519


If you get an output as below, you've successfully added your ssh key to the ssh-agent.

> Identity added: /c/Users/your.user.name/.ssh/id_ed25519 ([email protected])

Add your public SSH key to your GitHub account

Copy the SSH public key from the set of SSH keys you just generated and added to your ssh-agent. This is the step where the commands for Windows and Linux users would differ.


If you are a Windows user use the command below to copy the content of the public key file. The command copies the public key to your clipboard. You can verify by trying to paste it into a notepad.

$ clip < ~/.ssh/id_ed25519.pub


If you are a Linux use the command below to display the content of the public key file. Then, select and copy the long string displayed in the terminal to your clipboard.

$ cat ~/.ssh/id_ed25519.pub
> ssh-ed25519 ZZZZC3QzaC1lZDI1NTE5BBBBICiv7GatnQaTdOe1YoucU40UeoMLQOCHZP6LEpHuLOWg [email protected]


The next step is to log in to your GitHub account and go to the Settings page. In the user settings sidebar (on the left), click SSH and GPG keys


image

Click on the New SSH key green button.

image

In the "Title" field, add a descriptive label and paste your key into the "Key" field. Then, click on "Add SSH Key".

image

If prompted, confirm your GitHub password.

Now, verify the authentication with the below command.

ssh -T [email protected]


On successful authentication you will get the below output.

> Hi your.user.name! You've successfully authenticated, but GitHub does not provide shell access.

Push your project on your GitHub account

If you have created an empty repository on your GitHub account, you must be displayed with the Quick setup guide.


Follow the second part of the guide i.e. push an existing repository from the command line which shows the below command

git remote add origin [email protected]:your_user_name/your_repo_name.git
git branch -M main
git push -u origin main


If you want to deep dive into the details on authenticating with SSH please read the GitHubโ€™s documentation Connecting to GitHub with SSH. Thank you.

react to story with heart
react to story with light
react to story with boat
react to story with money
L O A D I N G
. . . comments & more!