Sphere
Log in Join

Git from Download to First Push: SSH Keys and the 'Permission Denied' Trap

A new teammate asked me why his git push kept saying "Permission denied (publickey)". He had downloaded Git, made an account, and followed a tutorial that stopped at git init. This is the setup I send people now, including the two checks that actually find the problem.

Download and install Git

Get Git from git-scm.com/download. Windows, macOS, and Linux installers are all on the front page; do not download it from a mirror you found in a search result. During the Windows install, keep "Git from the command line and also from 3rd-party software" selected. That is what puts git.exe on PATH.

Open a new terminal and check:

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

Use the email you want attached to commits, not necessarily the one you log in with. Git does not verify it.

Do not set up HTTPS with a password. GitHub removed password authentication for cloning years ago, and people still follow old tutorials that tell them to use it.

Make an SSH key

Run this on the machine you will actually push from:

ssh-keygen -t ed25519 -C "you@example.com"

Press Enter to accept the default path. Add a passphrase if this is your own laptop; skip it on a shared CI machine where nobody is around to type it.

Then copy the public key:

cat ~/.ssh/id_ed25519.pub

On Windows, type %USERPROFILE%\.ssh\id_ed25519.pub does the same thing. Copy the whole line, starting with ssh-ed25519. Do not copy the private key, which has no .pub suffix. I have seen that mistake twice.

In GitHub, go to Settings > SSH and GPG keys > New SSH key, paste it, and save. The title is just for you; "work laptop" is fine.

Test before cloning

This is the step most tutorials skip:

ssh -T git@github.com

The first time, you will see a fingerprint warning. Type yes. A successful result says "Hi username! You've successfully authenticated" and then complains that GitHub does not provide shell access. That complaint is normal; the authentication worked.

If you see Permission denied (publickey), run:

ssh -vT git@github.com

Look at the lines near the end. If it offers id_rsa but not id_ed25519, your agent is not loading the right key. On Windows the usual culprit is an old OpenSSH client; Git Bash includes its own. Run the test inside Git Bash and it often just works.

If it says "No such file or directory", you generated the key somewhere else or skipped the generation step.

Clone and push

Copy the SSH URL from GitHub, not the HTTPS one. It looks like git@github.com:user/repo.git.

git clone git@github.com:user/repo.git
cd repo
touch README.md
git add README.md
git commit -m "add readme"
git push -u origin main

If your default branch is master, use that instead. GitHub shows the branch name on the repository page, so check rather than assuming.

Two issues that still catch people

fatal: could not read Username for 'https://github.com' means the remote is still HTTPS. Change it:

git remote set-url origin git@github.com:user/repo.git
git remote -v

Updates were rejected because the remote contains work that you do not have locally is not an SSH problem at all. Pull first:

git pull --rebase origin main
git push

I keep a note of these because the error text sounds like authentication when it is not. Half of Git troubleshooting is reading the last line carefully and noticing which part actually failed.