codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
📦

Git & GitHub

56 / 63 topics
55Security Best Practices for Git and GitHub56Using SSH Keys with Git and GitHub57Enabling Two-Factor Authentication on GitHub58Auditing Repository Activity on GitHub
Tutorials/Git & GitHub/Using SSH Keys with Git and GitHub
📦Git & GitHub

Using SSH Keys with Git and GitHub

Updated 2026-04-20
3 min read

Introduction

In this tutorial, we will explore how to use SSH keys with Git and GitHub for secure authentication. SSH (Secure Shell) provides a secure way to access remote servers and services, including Git repositories hosted on GitHub. By using SSH keys, you can authenticate your identity without needing to enter your username and password every time.

Prerequisites

Before we dive into the tutorial, ensure that you have the following:

  • A GitHub account.
  • Git installed on your local machine. You can download it from git-scm.com.
  • Basic knowledge of Git commands.

Generating SSH Keys

The first step in using SSH keys with GitHub is to generate a new SSH key pair. Follow these steps:

  1. Open Terminal or Command Prompt: Depending on your operating system, open the terminal (Linux/Mac) or command prompt (Windows).

  2. Generate a New SSH Key:

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

    Replace your_email@example.com with your GitHub email address.

  3. Enter File Location: Press Enter to accept the default file location (~/.ssh/id_rsa).

  4. Set a Passphrase (Optional): You can set a passphrase for added security. If you choose to set a passphrase, remember it as you will need it later.

  5. Verify Key Generation: After generating the key pair, verify that they exist in the .ssh directory:

    ls -al ~/.ssh/id_rsa*
    

Adding SSH Key to the ssh-agent

The ssh-agent is a program that manages your SSH keys and keeps them secure. Here’s how to add your newly generated SSH key to the agent:

  1. Start the ssh-agent:

    eval "$(ssh-agent -s)"
    
  2. Add Your SSH Key to the Agent:

    ssh-add ~/.ssh/id_rsa
    

    If you set a passphrase, enter it when prompted.

Adding SSH Key to GitHub

Now that your SSH key is generated and added to the ssh-agent, you need to add it to your GitHub account:

  1. Copy Your SSH Public Key:

    cat ~/.ssh/id_rsa.pub
    

    Copy the output to your clipboard.

  2. Add SSH Key to GitHub:

    • Log in to your GitHub account.
    • Navigate to Settings > SSH and GPG keys.
    • Click on New SSH key.
    • Paste your public key into the "Key" field.
    • Give it a title (e.g., "Personal Laptop").
    • Click Add SSH key.

Testing Your SSH Connection

To ensure that everything is set up correctly, test your SSH connection to GitHub:

  1. Run the Test Command:

    ssh -T git@github.com
    
  2. Expected Output: You should see a message like this:

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

    Replace username with your GitHub username.

Configuring Git to Use SSH

By default, Git uses HTTPS for remote URLs. To switch to SSH, you need to update the remote URL of your repository:

  1. Check Current Remote URL:

    git remote -v
    
  2. Change Remote URL to SSH:

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

    Replace username and repository.git with your GitHub username and repository name.

Best Practices

  • Use Strong Passphrases: Always use a strong passphrase for your SSH keys to enhance security.
  • Keep Your Keys Secure: Do not share your private key with anyone. Keep it secure on your local machine.
  • Regularly Update Your Keys: Periodically generate new SSH keys and update them in GitHub for better security practices.

Conclusion

Using SSH keys with Git and GitHub provides a secure way to authenticate without exposing your credentials every time you interact with the remote repository. By following this comprehensive guide, you have successfully set up SSH keys, added them to the ssh-agent, configured them on GitHub, and tested your connection. This setup not only secures your interactions but also enhances the efficiency of your development workflow.

Additional Resources

  • GitHub Documentation: Connecting to GitHub with SSH
  • Git Documentation: SSH URLs

By leveraging these resources, you can further enhance your understanding and skills in using SSH keys with Git and GitHub.


PreviousSecurity Best Practices for Git and GitHubNext Enabling Two-Factor Authentication on GitHub

Recommended Gear

Security Best Practices for Git and GitHubEnabling Two-Factor Authentication on GitHub