How to clone Github repo using SSH

In this “cut to the chase” tutorial I’ll show you how to clone Github repo using ssh on Mac OS or Linux (so that you won’t need to use password).

Generate SSH key

If you don’t have any SSH key please run in your terminal:

ssh-keygen

This command will ask your for location & name for your key file. You can press enter or change it according to your preferences (I’ve typed github for the sake of this tutorial).

Now, go to your keys directory:

cd ~/.ssh/

And show .pub file contents:

cat github.pub

You should get something like:

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUp lot of mumbo jumbo C1yc2EAAAABIwAA

Copy this starting from ssh-rsa until final space.

Add a key to Github

Please login to your account and drop menu using your avatar in the top right corner then click Settings.

Go to SSH adn GPG keys on the left.

Github SSH keys settins page
Github settings – SSH & GPG keys

Now click New SSH key on top. Type name of your key (something like Work MacBook) and paste contents of your public key.

Let’s force your machine to use proper key for Github SSH connection. Go back to your terminal:

nano ~/.ssh/config

This should open/create your ssh config in a text editor. In a next step configure it like that:

Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/github
    PreferredAuthentications publickey

Remember to use correct key name! I’ve generated github and github.pub files.

Let’s check Github connection:

ssh -T [email protected]

If everything is OK. you should get:

Hi BadMotafuka! You've successfully authenticated, but GitHub does not provide shell access.

One final step for a great developer!

Remember this because it’s really important… From now on clone your repos using following url scheme:

ssh://[email protected]/<user>/<repository name>.git

Because it will not work if you’ll use conventional url.

Voilà! You can push to your repository without using password. Write a comment in case of any problems.




Comments

0 responses to “How to clone Github repo using SSH”

  1. Patrizio Bertoni Avatar
    Patrizio Bertoni

    This is great. I sent half an hour on the matter before finding you guide – then, suddenly, everything started working. Thank you!