Skip to main content

How to Add SSH Key to GitHub and Clone a Repository (Step-by-Step)

· 11 min read
Jagdish Kumawat
Founder @ Dewiride

Learn how to generate an SSH key, add it to your GitHub account, and clone a repository using SSH. This step-by-step guide covers macOS, Windows, and Linux with all the commands you need to set up passwordless GitHub authentication.

Introduction

If you are tired of entering your GitHub username and password every time you push, pull, or clone a repository, SSH key authentication is the solution. SSH (Secure Shell) lets you authenticate to GitHub using cryptographic keys instead of passwords — it is more secure, faster, and used by millions of developers worldwide.

In this guide, you will learn how to:

  • Generate an SSH key on your local machine using ssh-keygen.
  • Add your SSH key to the SSH agent so it is available automatically.
  • Add the public SSH key to your GitHub account via Settings.
  • Test the SSH connection to verify everything works.
  • Clone a GitHub repository using SSH instead of HTTPS.
  • Configure Git to use SSH by default for all GitHub operations.

By the end, you will have a fully working SSH setup for GitHub that works on macOS, Windows, and Linux.

Why Use SSH Keys for GitHub Instead of HTTPS?

Before diving into the setup, here is why SSH is the recommended authentication method for GitHub:

FeatureSSH Key AuthenticationHTTPS with Password/Token
SecurityStrong cryptographic key pair (Ed25519/RSA)Password or personal access token
ConvenienceNo credentials needed after setupMust enter or cache token each time
Brute-Force RiskNot vulnerablePasswords can be guessed
Setup EffortOne-time setupRepeated token management
Recommended ByGitHub official docsLegacy, being phased out

SSH keys provide stronger security, eliminate brute-force risks, and offer a seamless passwordless workflow once configured.

Prerequisites

Before you begin, make sure you have:

  1. A GitHub account (free or paid).
  2. Git installed on your local machine.
  3. A terminal application:
    • macOS: Terminal or iTerm2
    • Windows: Git Bash, PowerShell, or Windows Terminal
    • Linux: Any terminal emulator (GNOME Terminal, Konsole, etc.)

Step 1: Generate an SSH Key for GitHub

The first step is to generate an SSH key pair. This creates two files — a private key (stays on your machine, never shared) and a public key (added to GitHub).

Open your terminal and run the following command. Replace "your_email@example.com" with the email address associated with your GitHub account:

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

Here is what each flag does:

  • -t ed25519 — Uses the Ed25519 algorithm, which is the most secure and modern option recommended by GitHub.
  • -C "your_email@example.com" — Adds a comment label (your email) to identify the key.

When prompted:

  1. File location: Press Enter to accept the default path (~/.ssh/id_ed25519).
  2. Passphrase: Optionally enter a passphrase for extra security, or press Enter to skip.

After running the command, two files are created:

  • ~/.ssh/id_ed25519 — Your private key (keep this secret).
  • ~/.ssh/id_ed25519.pub — Your public key (this goes to GitHub).
note

If your system does not support Ed25519 (older systems), use RSA as a fallback:

Terminal
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

The RSA key files will be saved as ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub.

Step 2: Add the SSH Key to the SSH Agent

The SSH agent manages your keys in memory so Git can use them automatically without you having to specify the key file each time.

Start the SSH agent:

Terminal
eval "$(ssh-agent -s)"

You should see output like Agent pid 12345, confirming the agent is running.

Add your SSH key to the agent:

Terminal
ssh-add ~/.ssh/id_ed25519

macOS: Persist the Key Across Reboots

On macOS, the SSH agent does not retain keys after a reboot by default. To persist your key in the macOS Keychain, run:

macOS Terminal
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

You can also add this to your ~/.ssh/config file so macOS loads the key automatically:

~/.ssh/config
Host github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519

Windows: Start the SSH Agent Service

On Windows, if using PowerShell (not Git Bash), you may need to start the SSH agent service first:

PowerShell (as Administrator)
Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519

Step 3: Copy the SSH Public Key to Clipboard

Before adding the key to GitHub, you need to copy the public key to your clipboard.

macOS

macOS Terminal
pbcopy < ~/.ssh/id_ed25519.pub

Windows (Git Bash)

Git Bash
clip < ~/.ssh/id_ed25519.pub

Windows (PowerShell)

PowerShell
Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub | Set-Clipboard

Linux

Linux Terminal
xclip -selection clipboard < ~/.ssh/id_ed25519.pub

Manual Method (Any OS)

If the above commands do not work, display the key and copy it manually:

Terminal
cat ~/.ssh/id_ed25519.pub

The output will look like:

ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIG... your_email@example.com

Select and copy the entire output, including ssh-ed25519 at the start and your email at the end.

Step 4: Add the SSH Key to Your GitHub Account

Now add the copied public key to GitHub:

  1. Go to github.com and sign in to your account.

  2. Click your profile icon (top-right corner) → Settings.

GitHub Settings - Navigate to SSH Keys

  1. In the left sidebar, click SSH and GPG keys.

  2. Click the New SSH key button.

GitHub - Add New SSH Key

  1. Fill in the form:

    • Title: A descriptive name like "MacBook Pro", "Work Laptop", or "Ubuntu Desktop".
    • Key type: Select Authentication Key.
    • Key: Paste your public key (copied in Step 3).
  2. Click Add SSH key.

  3. GitHub may ask you to confirm your password. Enter it to save the key.

Your SSH key is now linked to your GitHub account and ready to use.

Step 5: Test the SSH Connection to GitHub

Before cloning any repository, verify that your SSH authentication is working correctly:

Terminal
ssh -T git@github.com

If prompted with Are you sure you want to continue connecting (yes/no)?, type yes and press Enter. This happens only the first time you connect.

Success output:

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

This confirms GitHub recognizes your SSH key.

warning

If you see Permission denied (publickey), troubleshoot with these steps:

  1. Verify the SSH agent is running:
    Terminal
    eval "$(ssh-agent -s)"
  2. Check that your key is loaded:
    Terminal
    ssh-add -l
    If the output says The agent has no identities, add your key:
    Terminal
    ssh-add ~/.ssh/id_ed25519
  3. Verify the public key in your GitHub account matches ~/.ssh/id_ed25519.pub.
  4. Run SSH in verbose mode to debug:
    Terminal
    ssh -vT git@github.com

Step 6: Clone a GitHub Repository Using SSH

With SSH configured, you can now clone any repository you have access to without entering credentials.

How to Find the SSH Clone URL

  1. Go to the repository on github.com.
  2. Click the green Code button.
  3. Select the SSH tab.
  4. Copy the URL (it starts with git@github.com:).

GitHub - Copy SSH Clone URL

Run the Git Clone Command

Open your terminal, navigate to the folder where you want the repository, and run:

Terminal
git clone git@github.com:username/repository.git

Replace username/repository.git with the actual path from GitHub. For example:

Terminal
git clone git@github.com:jagdish-kumawat/my-project.git

The repository will be cloned to a new folder named after the repository. Since SSH authentication is already configured, no username or password is required.

Clone into a Specific Folder

To clone into a custom directory name:

Terminal
git clone git@github.com:username/repository.git my-custom-folder

Step 7: Configure Git to Always Use SSH for GitHub

If you want Git to automatically use SSH for all GitHub operations — even when you copy an HTTPS URL — you can set a global URL rewrite rule:

Terminal
git config --global url."git@github.com:".insteadOf "https://github.com/"

This means that when you run a command like:

Terminal
git clone https://github.com/username/repository.git

Git will automatically convert it to:

Terminal
git clone git@github.com:username/repository.git

Verify the configuration:

Terminal
git config --global --get url."git@github.com:".insteadOf

Expected output: https://github.com/

This is especially useful in CI/CD pipelines and team environments where HTTPS URLs are commonly shared.

Summary: Quick Reference Commands

Here is a quick reference of all SSH setup commands in one place:

StepCommand
Generate SSH keyssh-keygen -t ed25519 -C "email@example.com"
Start SSH agenteval "$(ssh-agent -s)"
Add key to agentssh-add ~/.ssh/id_ed25519
Copy public key (macOS)pbcopy < ~/.ssh/id_ed25519.pub
Copy public key (Windows)clip < ~/.ssh/id_ed25519.pub
Copy public key (Linux)xclip -selection clipboard < ~/.ssh/id_ed25519.pub
Test SSH connectionssh -T git@github.com
Clone via SSHgit clone git@github.com:user/repo.git
Force SSH for GitHubgit config --global url."git@github.com:".insteadOf "https://github.com/"

Frequently Asked Questions

How do I check if I already have an SSH key?

Run the following command to list existing SSH keys:

Terminal
ls -al ~/.ssh

If you see files like id_ed25519 and id_ed25519.pub (or id_rsa and id_rsa.pub), you already have an SSH key pair. You can skip Step 1 and proceed directly to adding the public key to GitHub.

Can I use the same SSH key for multiple GitHub accounts?

No, GitHub does not allow the same SSH public key on two different accounts. You will need to generate a separate SSH key for each account and configure your ~/.ssh/config to specify which key to use:

~/.ssh/config
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal

Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work

Then clone using the custom host alias:

Terminal
git clone git@github-work:org/repository.git

What is the difference between Ed25519 and RSA SSH keys?

Ed25519 is newer, faster, and produces shorter keys while being more secure. RSA is older and widely supported but requires larger key sizes (4096-bit) for equivalent security. GitHub recommends Ed25519 for all new SSH keys.

How do I remove an SSH key from GitHub?

Go to GitHub Settings → SSH and GPG keys, find the key you want to remove, and click Delete. This revokes SSH access for that specific key immediately.

Why does git push still ask for a password after adding my SSH key?

This usually means your repository is using an HTTPS remote URL instead of SSH. Check with:

Terminal
git remote -v

If the URL starts with https://, switch it to SSH:

Terminal
git remote set-url origin git@github.com:username/repository.git

Is it safe to use SSH keys without a passphrase?

An SSH key without a passphrase is still significantly more secure than password authentication. However, if someone gains access to your machine, they can use the key. Adding a passphrase provides an extra layer of protection. For most personal development machines, a passphrase is optional but recommended for shared or work machines.

Conclusion

Setting up SSH key authentication for GitHub is a one-time process that permanently improves your development workflow. By generating an Ed25519 SSH key, adding it to the SSH agent, and linking it to your GitHub account, you get secure, passwordless access to all your repositories.

Whether you are cloning repositories, pushing code, or deploying through CI/CD pipelines, SSH authentication makes every Git operation faster and more secure. Follow the steps above for your operating system — macOS, Windows, or Linux — and you will never need to enter GitHub credentials again.

For official reference, visit the GitHub SSH documentation.

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.