Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Git for Windows does not use the settings in your SSH config

ErrorPermission denied (publickey).

Short answer

Git does not read ssh config itself. It starts an ssh program, and only OpenSSH reads ~/.ssh/config. If core.sshCommand, GIT_SSH_COMMAND or GIT_SSH points at plink or another client, your config is skipped. Also check that the Host pattern matches the name in the remote URL and that an earlier block is not winning.

  • Applies to Git for Windows 2.4x, OpenSSH for Windows, OpenSSH 9.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out which ssh program Git is told to use. If any of these print something, Git is not using plain ssh.

    git config --show-origin --get core.sshCommand
    echo $GIT_SSH_COMMAND $GIT_SSH
    git remote -v
  2. 2Match what you see, then jump to its fix

    1. 1core.sshCommand, GIT_SSH_COMMAND or GIT_SSH is set, often to plink.exe or to a path from an old setup.Git is pointed at a different ssh programFix
    2. 2Git connects with the default key because the URL still says github.com.The remote URL does not use the Host alias you definedFix
    3. 3Permission denied (publickey) or the wrong account name in the greeting, although IdentityFile names the right key.The config is read, but another key from the agent is offered firstFix

What does the error look like?

  • Permission denied (publickey).

Cause 1 of 3

Git is pointed at a different ssh program

1Check

GIT_SSH_COMMAND beats GIT_SSH, and Git adjusts the command-line arguments to the ssh variant (OpenSSH, plink or tortoiseplink). Only the OpenSSH client reads ~/.ssh/config, so check that the command named here is an OpenSSH ssh.

git config --show-origin --get-all core.sshCommand
echo $GIT_SSH_COMMAND

2Fix

Remove the override, or point Git at the Windows OpenSSH client that reads your config.

git config --global --unset core.sshCommand
# or use the Windows client explicitly:
git config --global core.sshCommand "C:/Windows/System32/OpenSSH/ssh.exe"

3Verify

Run a clone that needs the alias, and watch the connection details.

ssh -vT git@work-github

Confirmed by git-scm.com

Did this fix it?

Cause 2 of 3

The remote URL does not use the Host alias you defined

1Check

A Host block only applies when the name on the command line matches its pattern. If the config defines Host work-github but the remote says git@github.com, the block never matches.

git remote -v
grep -n -A5 "^Host" ~/.ssh/config

2Fix

Make the remote use the alias, and put the specific block above any general Host * block, because ssh keeps the first value it finds.

git remote set-url origin git@work-github:org/repo.git

# ~/.ssh/config
Host work-github
    Hostname github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_work
    IdentitiesOnly yes

Host *
    ServerAliveInterval 60

3Verify

The test greets the account that owns the work key.

ssh -T git@work-github

Confirmed by man.openbsd.org

Did this fix it?

Cause 3 of 3

The config is read, but another key from the agent is offered first

1Check

IdentityFile only adds a file to the list. Unless IdentitiesOnly is yes, ssh may still offer keys held by the agent first.

ssh -i ~/.ssh/id_ed25519_work -o IdentitiesOnly=yes -vT git@github.com

2Fix

Add IdentitiesOnly yes to that Host block, as in the example above, or set it for one repository with core.sshCommand.

git config core.sshCommand "ssh -i ~/.ssh/id_ed25519_work -o IdentitiesOnly=yes"

3Verify

Fetch from the repository.

git fetch origin

Confirmed by man.openbsd.org

Did this fix it?

How do I stop it coming back?

  • Keep one ssh program for Git on the machine, and avoid setting GIT_SSH in system variables.
  • Use a Host alias per account and put every alias above Host *.
  • Check git remote -v after cloning with an alias, since a copied URL from the website uses github.com.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.