Software >> Services >> Version Control Systems >> git >> How to use SSH key authentication with github repo

 

On Linux

ssh-keygen -t ed25519 -C "your-git-hub-email@example.com"

eval "$(ssh-agent -s)"

take note of the private key
cat ~/.ssh/id_ed25519

and public key
cat ~/.ssh/id_ed25519.pub

## copy your pub key to clipboard

## login to your github account

Click your account (top right corner)
-> Settings -> SSH and GPG Keys -> New SSH key

give it a name and paste the public key from clipboard


Now when connecting to the remote repo in github use

git@github.com:youraccount/yourrepo.git

instead of https://github.com/youraccount/yourrepo.git as the URL

This will make the connection use SSH with key authentication

On Windows (git for windows)


## Launch git bash, create the SSH key

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


## it will create id_ed25519 (private) and id_ed25519.pub (public) key files in .ssh subfolder of your home directory
## copy the public key contents into your github accounts SSH Keys store as described here
## If connecting to only one github account configure your global or per repository git username and email

git config --global user.name "John Doe"

git config --global user.email "johndoe@example.com"


## On your github account, select the desired repository to connect to clone, select the SSH URL to use for that reposity
## it should be git@github.com:[youraccountname]/[yourrepo]




## with your SSH private key at the default location, you are ready to connect to your Github repo using SSH
## clone as follows

git clone git@github.com:[youraccountname]/[yourrepo]

 

 

Reference: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent