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
- 🔹 What is an SSH Key and Why Use It?
- ⚡ Generating an SSH Key Pair
- 🔐 Adding the SSH Key to the SSH Agent
- 🔗 Adding the SSH Key to GitHub
- ⚙️ Configuring Git to Use SSH
- 🔽 Cloning a Repository Using SSH
- 🚀 Pushing Code to GitHub Using SSH
- 🔑 Managing Multiple SSH Keys
- 🎯 Conclusion
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.
✅ 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.
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 ~/.sshTo avoid entering a passphrase every time, add your SSH key to the SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519To verify:
ssh-add -l1️⃣ Copy the SSH Public Key:
cat ~/.ssh/id_ed25519.pub | clip2️⃣ 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.
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"To clone a GitHub repository using SSH:
git clone git@github.com:your-username/repository.git💡 Verify SSH Remote URL:
git remote -vExpected Output:
origin git@github.com:your-username/repository.git (fetch)
origin git@github.com:your-username/repository.git (push)
echo "Hello, GitHub!" > example.txt
git statusgit add example.txt
git commit -m "Added example.txt"git push origin main💡 If it's your first push, set the upstream branch:
git push --set-upstream origin mainIf 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_work2️⃣ Edit the SSH config file (~/.ssh/config):
nano ~/.ssh/configAdd 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_personal3️⃣ 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.gitSetting 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