August 25, 2025

Stop Git Asking for Username and Password Use SSH Keys Instead

When you run commands like:

git fetch origin live

or

git pull origin main

Git may prompt you for a username and password. This isn’t just annoying — it can break automated deployments because servers can’t type passwords for you.

Modern Git hosting platforms like GitHub, GitLab, and Bitbucket have already disabled password authentication in favor of SSH keys or Personal Access Tokens (PATs). For servers, SSH keys are by far the better option — secure, permanent, and password-free.


Why use SSH keys?

  • No password prompts — perfect for scripts, CI/CD pipelines, or webhook deployments.
  • More secure than HTTPS passwords or embedded tokens.
  • One-time setup — after adding your server key to GitHub/GitLab, it just works.

Step 1: Generate an SSH key on your server

SSH keys come in a pair — a private key (kept secret on your server) and a public key (shared with GitHub/GitLab).

Run this on your server:

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

  • Press Enter to accept the default path (~/.ssh/id_ed25519).
  • You can leave the passphrase empty if the server needs to authenticate automatically.

This will create:

  • ~/.ssh/id_ed25519 — private key
  • ~/.ssh/id_ed25519.pub — public key

Step 2: Add the public key to GitHub or GitLab

Display your public key:

cat ~/.ssh/id_ed25519.pub

Copy everything that appears (it starts with ssh-ed25519).

For GitHub:

  1. Go to Settings → SSH and GPG Keys → New SSH Key
  2. Paste your key and click Add SSH key

For GitLab:

  1. Go to Preferences → SSH Keys → Add key
  2. Paste your key and save

Step 3: Test your SSH connection

Run:

ssh -T git@github.com

If successful, you’ll see:

Hi username! You've successfully authenticated...

(For GitLab, replace git@github.com with git@gitlab.com.)


Step 4: Update your Git remote to use SSH

In your project folder:

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

or for GitLab:

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


Now your server is ready for password-free Git commands

From this point on, commands like:

git fetch origin live
git pull origin main

will work without any username or password prompt — perfect for deployment scripts, PM2 restarts, or webhook-driven updates.