Skip to content

shafiqul-islam-sumon/Github_SSH_Setup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

🔑 SSH Key Generation and Adding It to GitHub

Setting up SSH authentication for GitHub allows you to securely clone repositories, push code, and manage projects without repeatedly entering credentials. This guide walks you through generating an SSH key, adding it to GitHub, and configuring Git to use SSH instead of HTTPS.

📌 For a detailed step-by-step guide, check out the Full Blog Post: 👉 How to Generate an SSH Key and Add It to GitHub for Secure Git Operations

📌 Table of Contents

🔹 What is an SSH Key and Why Use It?

SSH (Secure Shell) keys provide password-free authentication between your system and GitHub using public-key cryptography. This eliminates the need for credentials every time you interact with a repository.

✨ Why Use SSH Keys?

Increased Security – Encrypted authentication, no risk of leaked passwords. ✅ Seamless Workflow – No need to enter credentials repeatedly. ✅ GitHub Recommended – GitHub no longer supports password-based Git operations.

⚡ Generating an SSH Key Pair

Run the following command to create a secure Ed25519 SSH key (recommended by GitHub):

ssh-keygen -t ed25519 -C "your-email@example.com"

💡 What Happens?

  • Generates a private key (id_ed25519) stored on your machine.
  • Generates a public key (id_ed25519.pub) that you add to GitHub.

📌 To list existing SSH keys:

ls -al ~/.ssh

🔐 Adding the SSH Key to the SSH Agent

To avoid entering a passphrase every time, add your SSH key to the SSH agent:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

To verify:

ssh-add -l

🔗 Adding the SSH Key to GitHub

1️⃣ Copy the SSH Public Key:

cat ~/.ssh/id_ed25519.pub | clip

2️⃣ Go to GitHub → Settings → SSH and GPG Keys 3️⃣ Click "New SSH Key" → Paste the key → Save 4️⃣ Test Connection:

ssh -T git@github.com

💡 Expected Output:

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

⚙️ Configuring Git to Use SSH

To ensure Git always uses SSH instead of HTTPS, run:

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

📌 Set Your Git User Info:

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

🔽 Cloning a Repository Using SSH

To clone a GitHub repository using SSH:

git clone git@github.com:your-username/repository.git

💡 Verify SSH Remote URL:

git remote -v

Expected Output:

origin  git@github.com:your-username/repository.git (fetch)
origin  git@github.com:your-username/repository.git (push)

🚀 Pushing Code to GitHub Using SSH

Step 1: Make Changes & Check Status

echo "Hello, GitHub!" > example.txt
git status

Step 2: Add & Commit Changes

git add example.txt
git commit -m "Added example.txt"

Step 3: Push to GitHub

git push origin main

💡 If it's your first push, set the upstream branch:

git push --set-upstream origin main

🔑 Managing Multiple SSH Keys

If you have multiple GitHub accounts (work & personal), configure SSH to handle different keys.

1️⃣ Generate a second SSH key:

ssh-keygen -t ed25519 -C "work-email@example.com" -f ~/.ssh/id_ed25519_work

2️⃣ Edit the SSH config file (~/.ssh/config):

nano ~/.ssh/config

Add the following:

# Work GitHub Account
Host github-work
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work

# Personal GitHub Account
Host github-personal
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_personal

3️⃣ Clone repositories using the correct key:

git clone git@github-work:work-username/work-repo.git
git clone git@github-personal:personal-username/personal-repo.git

🎯 Conclusion

Setting up SSH authentication for GitHub makes your Git workflow faster and more secure. By following these steps, you can clone repositories, push code, and manage multiple accounts seamlessly. With SSH, you no longer need to enter your credentials for every Git operation, ensuring a frictionless coding experience.

📌 For a more detailed guide, visit following blog post: 👉 How to Generate an SSH Key and Add It to GitHub for Secure Git Operations