DEV Community

slatzz
slatzz

Posted on

How To: Clone Github Repo with SSH (Mac)

After a year of using Bitbucket, I ran into this error when attempting to clone a Github repository:

Cloning into 'YOUR_PROJECT'...
Username for 'https://github.com': YOUR_GITHUB_USERNAME
Password for 'https://YOUR_GITHUB_USERNAME@github.com':
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
fatal: Authentication failed for 'https://github.com/YOUR_DIRECTORY/YOUR_PROJECT.git/'
Enter fullscreen mode Exit fullscreen mode

Since password authentication isn't available, let's clone a repo using SSH. You will need to open:

  • Your Github settings
  • iTerm or preferred terminal

We begin in the terminal.

Generate your SSH keys

// cd into your .ssh folder from your home directory (~)

> cd
> cd .ssh
> ssh-keygen -b 4096 -t rsa

// Complete the following prompts:
Generating public/private rsa key pair.
Enter file in which to save the key
(/Users/YOUR_USER_NAME/.ssh/id_rsa): YOUR_PROJECT
// You can press ENTER for no passphrase
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in YOUR_PROJECT
Your public key has been saved in YOUR_PROJECT.pub
The key fingerprint is: ...

// Let's check your new ssh keys have been added.
// You should still be in the .ssh directory
> ls

// You should see your YOUR_PROJECT twice as the following:
YOUR_PROJECT
YOUR_PROJECT.pub

// To view their contents, run:
> cat YOUR_PROJECT
or
> cat YOUR_PROJECT.pub

// This is important, we will return to this in a moment.
Enter fullscreen mode Exit fullscreen mode

Add your public SSH key to Github

Now you have created your ssh keys, let's return to Github.

On the right, click your profile photo > Settings > SSH and GPG Keys.

Image description

Select New SSH Key

Image description

Image description

Return to your terminal.

// From within your .ssh directory, run:
cat YOUR_PROJECT.pub
// This is your public ssh key. Copy it and paste in the Key box pictured above.
Enter fullscreen mode Exit fullscreen mode

Title your key (it's good to title it after your project name) and click Add SSH Key when you're done.

Go to your repo's page and copy the link to clone with SSH. It will look something like git@github.com:YOUR_DIRECTORY/YOUR_PROJECT.git

Go back to your terminal. We are almost done!

Clone your repo

// You are probably still in your .ssh directory. You will want to now go into the directory into which you want to clone your repo. 
> cd
> cd YOUR_TARGET_DIRECTORY
> ssh-add ~/.ssh/YOUR_PROJECT
Identity added: /Users/YOUR_USER_NAME/.ssh/YOUR_PROJECT
> git clone git@github.com:YOUR_DIRECTORY/YOUR_PROJECT.git
Cloning into 'YOUR_PROJECT'...
remote: Enumerating objects: 67837, done.
remote: Counting objects: 100% (358/358), done.
remote: Compressing objects: 100% (194/194), done.
remote: Total 67837 (delta 226), reused 262 (delta 163), pack-reused 67479
Receiving objects: 100% (67837/67837), 33.61 MiB | 19.82 MiB/s, done.
Resolving deltas: 100% (53811/53811), done.
// Yay! 


## Heads up
///////////////////////////////////////////
// Note! If at any point you get this message:
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights

// Run the following again inside YOUR_TARGET_DIRECTORY
> ssh-add ~/.ssh/YOUR_PROJECT
// Make sure you see this:
Identity added: /Users/YOUR_USER_NAME/.ssh/YOUR_PROJECT

// Then try to clone or run project again.
Enter fullscreen mode Exit fullscreen mode

Thanks for reading! I hope this was helpful! 🎉

Top comments (0)