How to Set Up SSH Keys in Git: A Step-by-Step Guide

SSH keys provide a secure way to authenticate with remote Git repositories without repeatedly entering your username and password. Setting up SSH keys is highly recommended, especially for frequent Git operations.

In this guide, you’ll learn how to generate, add, and configure SSH keys for Git on your system.


Why Use SSH Keys?

  • Password-less, secure authentication
  • Faster Git operations (no password prompts)
  • Enhanced security over HTTPS with passwords

Step 1: Check for Existing SSH Keys

Open your terminal (Git Bash, Command Prompt, or Terminal) and run:

ls -al ~/.ssh

Look for files like id_rsa and id_rsa.pub or id_ed25519 and id_ed25519.pub.

If these exist, you can reuse them; otherwise, proceed to generate a new key.


Step 2: Generate a New SSH Key

Run this command to create a new SSH key using the modern Ed25519 algorithm:

ssh-keygen -t ed25519 -C "yo********@ex*****.com"

If your system doesn’t support Ed25519, use RSA instead:

ssh-keygen -t rsa -b 4096 -C "yo********@ex*****.com"
  • When prompted, press Enter to accept the default file location.
  • Optionally, set a passphrase for extra security.

Step 3: Start the SSH Agent

Make sure the SSH agent is running to manage your keys:

  • On Linux/macOS: eval "$(ssh-agent -s)"
  • On Windows (Git Bash): eval $(ssh-agent -s)

Step 4: Add Your SSH Key to the SSH Agent

Add the private key to the agent:

ssh-add ~/.ssh/id_ed25519

Or if you generated an RSA key:

ssh-add ~/.ssh/id_rsa

Step 5: Add the SSH Public Key to Your Git Hosting Service

Get your public key content:

cat ~/.ssh/id_ed25519.pub

(or id_rsa.pub)

Copy the entire output (starts with ssh-ed25519 or ssh-rsa).

Add to GitHub:

  • Go to GitHub > Settings > SSH and GPG keys.
  • Click New SSH key, paste the key, give it a title, and save.

Add to GitLab:

  • Go to GitLab > User Settings > SSH Keys.
  • Paste your public key and save.

Add to Bitbucket:

  • Go to Bitbucket > Personal Settings > SSH Keys.
  • Add the new key.

Step 6: Test Your SSH Connection

Run:

ssh -T gi*@gi****.com

(Replace github.com with your Git host if different.)

You should see a success message like:

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

Summary

StepCommand/Action
Check existing keysls -al ~/.ssh
Generate SSH keyssh-keygen -t ed25519 -C "yo********@ex*****.com"
Start SSH agenteval "$(ssh-agent -s)"
Add SSH key to agentssh-add ~/.ssh/id_ed25519
Copy public key contentcat ~/.ssh/id_ed25519.pub
Add key to Git hosting servicePaste in GitHub/GitLab/Bitbucket SSH keys settings
Test connectionssh -T gi*@gi****.com

Conclusion

Setting up SSH keys simplifies and secures your Git workflow by enabling password-less authentication. Once configured, you can push, pull, and clone repositories smoothly and securely.

Sharing Is Caring:

Leave a Comment