Post

GitHub: How to clone a repository with SSH

GitHub doesn't recommend using HTTPS to clone repositories. Let me show you how to clone a repository with SSH.

GitHub: How to clone a repository with SSH

GitHub Docs: Connecting to GitHub with SSH

ItemVersion
OSMacOS sequoia 15.3.1
Git2.49.0
OpenSSH9.8p1

1. Generate SSH Key

1
2
# -t: key type, -C: comment, -f: file location, -N: passphrase
ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/id_ed25519_github_authentication -N ""

2. Add SSH Key to GitHub

2.1. Copy Public Key to Clipboard

1
cat ~/.ssh/id_ed25519_github_authentication.pub | pbcopy # macOS

2.2. Open GitHub Settings

  1. Go to GitHub Settings
  2. Click New SSH key Github Settings
  3. Select Key type as Authentication Key and paste the public key to the Key field New SSH key
  4. Click Add SSH key

3. Set SSH Config

Open ~/.ssh/config file.

Then, add the following content to the file.

1
2
3
4
Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github_authentication

You can set any name for the host. Additionally, you can set multiple github accounts on the same machine, if you add public keys for each github account.

1
2
3
4
5
6
7
8
9
Host github-personal
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github_authentication

Host github-company
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_ed25519_github_authentication

4. Test SSH Connection

1
ssh -T github-personal

5. Clone Repository with SSH

1
git clone github-personal:<your_username>/<your_repository>.git

Let’s examine the default settings of GitHub SSH. If you set SSH key as id_ed25519 which is default and not edited SSH config, you can clone repository with the following command.

1
git clone git@github.com:<your_username>/<your_repository>.git

And your remote repository name is origin, not github-personal.

This post is licensed under CC BY 4.0 by the author.