TechEarl

How to Add an SSH Key to GitHub and GitLab

Paste your SSH public key into GitHub or GitLab, then confirm it works with ssh -T. The whole flow, with no password or token prompts on git push after.

Ishan Karunaratne⏱️ 4 min readUpdated
Share thisCopied
Add your SSH public key to GitHub or GitLab and verify it with ssh -T so git clone, pull and push stop asking for credentials.

Adding an SSH key to GitHub or GitLab is the same idea as adding it to a server: the public half goes on their side, the private half stays on yours. The only difference is you paste it into a settings page instead of running ssh-copy-id.

Copy your public key

You need an SSH key first. Copy the public half to the clipboard:

Try it with your own values

Pick your OS to get the right clipboard command.

bash· Linux (GNU)
xclip -sel clip < ~/.ssh/id_ed25519.pub   # or: wl-copy < ~/.ssh/id_ed25519.pub

If those tools are not installed, just cat ~/.ssh/id_ed25519.pub and copy the single line by hand. It starts with ssh-ed25519 and ends with your comment.

Paste it into the web UI

  • GitHub: Settings, then SSH and GPG keys, then New SSH key. Give it a title (the machine name is useful), leave the type as Authentication Key, paste, and save.
  • GitLab: Preferences, then SSH Keys, then Add new key. Paste, optionally set an expiry, and save.

You are pasting the contents of the .pub file. Never paste the private key (the file with no extension).

Test it from the terminal

This is the step people skip and then wonder why git push still fails:

bash
# GitHub
ssh -T git@github.com

# GitLab
ssh -T git@gitlab.com

GitHub replies Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access. That sentence is success, not an error. GitLab says Welcome to GitLab, @USERNAME!. The first time you connect you will be asked to trust the host key; type yes.

Use SSH for the remote, not HTTPS

A key only helps if the repo uses the SSH remote URL (git@...), not HTTPS:

bash
# Check the current remote
git remote -v

# Switch an existing repo from HTTPS to SSH
git remote set-url origin git@github.com:owner/repo.git

Clone with the SSH URL from the start (the green Code button, SSH tab) and git clone, pull, and push use the key automatically with no prompts.

FAQ

See also

Sources

Authoritative references this article was fact-checked against.

TagsSSHGitHubGitLabgitssh-keygenDevOps

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Tech Architect · Software Engineer · AI/DevOps

Tech architect and software engineer with 20+ years building software, Linux systems, and DevOps infrastructure, and lately working AI into the stack. Currently Chief Technology Officer at a healthcare tech startup, which is where most of these field notes come from.

Keep reading

Related posts

How to Add an EBS Volume to an EC2 Instance

Create an EBS volume, attach it to a running EC2 instance, format and mount it, and survive reboots with a UUID-based fstab entry. Console, AWS CLI, and Terraform walkthroughs plus the Nitro device-naming gotcha that trips everyone.

How to Create an SSH Key in 2026

Create an SSH key in one ssh-keygen command. Which key type to pick in 2026 (Ed25519 vs RSA), whether to set a passphrase, and the file permissions SSH needs, on Linux, macOS and Windows.