Step-by-Step Guide to Linking an SSH Key with Your GitHub Repositories

Ummer FarooqUmmer Farooq
2 min read

1. Generate a New SSH Key

Run the following command to generate a new SSH key specifically for GitHub:

ssh-keygen -t ed25519 -C "your_email@example.com" -f ~/.ssh/id_github

Explanation:

  • -t ed25519: Specifies the key type.

  • -C "your_email@example.com": Adds a comment to the key (typically your email).

  • -f ~/.ssh/id_github: Specifies the file name for the new key.

During the process, you’ll be prompted to:

  1. Confirm the file location (press Enter to accept the default).

  2. Enter a passphrase (optional, but recommended for added security).

2. Verify the Key Files

Once the SSH key is created, confirm the existence of the key files using:

ls -l ~/.ssh/id_github*

You should see:

  • ~/.ssh/id_github: The private key (keep this secure and never share it).

  • ~/.ssh/id_github.pub: The public key (this will be added to GitHub).

3. Add the Key to Your SSH Agent

Add the private key to your SSH agent to manage it easily:

  1. Start the SSH agent:

     eval "$(ssh-agent -s)"
    
  2. Add the key to the agent:

     ssh-add ~/.ssh/id_github
    

4. Copy the Public Key to Your Clipboard

To add the key to GitHub, first copy the public key:

cat ~/.ssh/id_github.pub

5. Add the SSH Key to Your GitHub Account

  1. Log in to your GitHub account.

  2. Navigate to Settings > SSH and GPG keys.

  3. Click New SSH key.

  4. Paste your public key into the provided field.

  5. Add a title to identify this key (e.g., "Server").

  6. Click Add SSH key.

6. Test the SSH Connection

Verify the connection to GitHub using:

ssh -i ~/.ssh/id_github -T git@github.com

If successful, you should see a message like:

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

7. Using the SSH Key for Repository Management

Clone a Repository

Use the SSH URL to clone a repository:

When pushing changes, Git will automatically use the SSH key for authentication:

git clone git@github.com:<username>/<repository>.git

Push Changes

git push origin main

Conclusion

By setting up an SSH key with GitHub, you’ve simplified your authentication process and secured your repository management workflow. No more password prompts—just smooth, secure Git operations!

0
Subscribe to my newsletter

Read articles from Ummer Farooq directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ummer Farooq
Ummer Farooq