Adding ssh-keys to GitHub account
For security reasons on GitHub, now users have to verify themselves before they can push, commit, etc on their GitHub account using Git.
Otherwise, you will get an error when you try to push your files to GitHub - "You do not have access to this repository. Check your rights."
There are 2 ways to verifications of users:
By adding ssh-keys, and
By adding PAT (Personal Access Token), when you set up a remote repository URL.
Steps to add ssh-keys:
#Generate the ssh-key ssh-keygen -t ed25519 -C "your_email@example.com"
#Location where your ssh key will be saved Enter a file in which to save the key (/home/YOU/.ssh/id_ALGORITHM):[Press enter]
#You can enter the passphrase if you want to, otherwise you can leave it empty by just using Enter Enter passphrase (empty for no passphrase): [Enter] Enter same passphrase again: [Enter]
#Before storing the ssh-key to ssh-agent, you have to start the ssh-agnet in the background ssh-agent bash
#If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_ed25519 in the command with the name of your private key file. ssh-add ~/.ssh/id_ed25519
Now add the ssh-keys to your GitHub account.
#Copy the public key of your generated ssh key cat ~/.ssh/id_ed25519.pub # Then select and copy the contents of the id_ed25519.pub file # displayed in the terminal to your clipboard
Go to your GitHub account > Choose Setting > Select SSH and GPG keys.
Click New SSHKey or Add SSH Key. Enter the name of the key (whatever you want) in the Title field. Select the type of key, authenticating or signing.
In the key field, copy your public key.
Click Add SSH Key.
Steps to add PAT:
Go to your GitHub account > Choose Setting > Select or search for the Developer Setting at the bottom.
-
Click on Personal Access Tokens > select Tokens(classic).
Click on Generate New Token. Enter your password for verification.
Select the expiration time of your token, and click on Generate Token.
Copy the generated token in a file, as you will not able to see it again for security reasons.
How you can use PAT to push your code/files to GitHub
Before using "git push", you have to add the generated PAT token when you set up the remote URL.
git remote add origin https://your_PAT@github.com/your_repository_address.git
Facing the following error when pushing the code to GitHub -
ssh: connect to host github.com port 22: No route to host
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
Solution :Change your internet connection. Sometimes if you are using your hostels/office/school internet service, they may have some firewall enabled that stops your connection to the GitHub.
For more information, read the official documentation on GitHub.
Subscribe to my newsletter
Read articles from Himanshu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by