Skip to main content

Setting Up SSH Key for Git

Introduction

This guide will show you how to set up an SSH key for Git, enabling you to authenticate with Git repositories without entering your username and password every time.

Generate SSH Key

Windows

  1. Open Git Bash: Launch Git Bash from your Start menu.

  2. Generate SSH Key: Run the following command, replacing your_email@email.com with your actual email address:

    ssh-keygen -t rsa -b 4096 -C "your_email@email.com"
  3. Save the Key: Press Enter to accept the default file location (/c/Users/your_username/.ssh/id_rsa).

  4. Enter a Passphrase: Type a secure passphrase to protect your SSH key. Remember this passphrase as it will be needed to use your key.

Linux & macOS

  1. Open Terminal: Launch a terminal window.

  2. Generate SSH Key: Run the following command, replacing your_email@email.com with your actual email address:

    ssh-keygen -t rsa -b 4096 -C "your_email@email.com"
  3. Save the Key: Press Enter to accept the default file location (/home/your_username/.ssh/id_rsa on Linux or /Users/your_username/.ssh/id_rsa on macOS).

  4. Enter a Passphrase: Type a secure passphrase to protect your SSH key. Remember this passphrase as it will be needed to use your key.

Add SSH Key to SSH Agent

Windows

  1. Start the SSH Agent: In Git Bash, run:

    eval $(ssh-agent -s)
  2. Add Your SSH Key: Run the following command:

    ssh-add /c/Users/your_username/.ssh/id_rsa

Linux & macOS

  1. Start the SSH Agent: In a terminal, run:

    eval $(ssh-agent -s)
  2. Add Your SSH Key: Run the following command:

    ssh-add ~/.ssh/id_rsa

Adding SSH Key to Your Git Hosting Service

GitHub

  1. Copy Your SSH Key: Copy the SSH key to your clipboard:

    cat ~/.ssh/id_rsa.pub
  2. Add SSH Key to GitHub:

    • Go to GitHub and log in to your account.
    • Navigate to Settings > SSH and GPG keys > New SSH key.
    • Paste your SSH key into the "Key" field and add a descriptive title.
    • Click Add SSH key.

GitLab

  1. Copy Your SSH Key: Copy the SSH key to your clipboard:

    cat ~/.ssh/id_rsa.pub
  2. Add SSH Key to GitLab:

    • Go to GitLab and log in to your account.
    • Navigate to Profile settings > SSH Keys.
    • Paste your SSH key into the "Key" field and add a descriptive title.
    • Click Add key.

Testing SSH Connection

  1. Test the SSH Connection: Run the following command to verify that your SSH key is set up correctly:

    ssh -T git@github.com

    or for GitLab:

    ssh -T git@gitlab.com

If successful, you should see a message indicating that authentication was successful.