Setup Github ssh key with git/github: Clone private repo using ssh

leangaurav
3 min readDec 30, 2022

--

Using ssh key allows you to work with private repos easily without username password authentication. Github now has an option for PAT (personal access token, read about it here), but I still use ssh key option.

Step-1: Install Git

Mac/Linux: git should be pre-installed.
Windows: download and install git bash from here.

Step-2: Generate a new ssh key

If you already have a key pair, skip this step and use the same keys. Otherwise, use below command to generate a new public-private key pair. Works on windows, mac and linux.
Replace youremail@something.com with your github email id.

ssh-keygen -t ed25519 -C "youremail@something"

Notice it’s capital C .

keygen on windows

When asked for location and file name, hit enter to save to default location.

passphrase prompt

When asked for passphrase, either type a secret passphrase and hit Enter, or to leave empty hit Enter twice.

This generates 2 files:

  1. A public key ending with .pub
  2. A private key without any extension (this is secret and should never be shared)

Step-2.1: Add key to ssh agent (for windows only)

Open git bash

Run this command (Use Shift + Ins to paste in bash)

eval "$(ssh-agent -s)"

You’ll see the process id

Add the newly generated key to ssh agent. (Use the same file which was generated in previous step)

ssh-add ~/.ssh/id_rsa

Step-3: Add ssh key to github

Copy the newly generated public key

cat ~/.ssh/id_rsa.pub

Above command works on Mac and Linux. On windows it works in powershell and git-bash. Copy the printed output as it is, including the starting ssh-rsa .

Now go to this url https://github.com/settings/keys

Or manually navigate:
1. click top right profile icon -> settings

click top right profile icon -> settings

2. Click “ssh and gpg keys” in left panel

Click “ssh and gpg keys” in left panel

3. Click New SSH key

4. Add Titleof your choice -> leave the Key type set to Authentication Key -> Paste the copied public key in Key section

Hit the green button.

Done 🙌🏻. You should be able to clone, pull-push to private repos with the ssh remote url.

Hope this helped😀… consider hitting 👏. Saved your precious time !! Appreciate my efforts buy me a tea ☕. You can also find me on Linkedin and say 👋

If you changed default ssh key location to something else

Assuming you named it something like

.ssh/id_rsa_github

Then you will still get this error if you try to clone:

Then you need to edit the ssh config file usually foundd under .ssh/config

vi .ssh/config

If the file doesn’t exist already, you can create it in .ssh folder. Then paste the following, replacing the file name

Host github.com
IdentityFile ~/.ssh/id_rsa_git

And done !!

--

--

leangaurav

Engineer | Trainer | writes about Practical Software Engineering | Find me on linkedin.com/in/leangaurav | Discuss anything topmate.io/leangaurav