Git for Windows does not use the settings in your SSH config
Permission 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.
Fix it fast
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 -v2Match what you see, then jump to its fix
- 1
core.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
Git connects with the default key because the URL still says github.com.The remote URL does not use the Host alias you definedFix - 3
Permission 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
- 1
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_COMMAND2Fix
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-githubConfirmed by git-scm.com
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/config2Fix
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 603Verify
The test greets the account that owns the work key.
ssh -T git@work-githubConfirmed by man.openbsd.org
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.com2Fix
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 originConfirmed by man.openbsd.org
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
Security
Entra ID token request fails: the request body must contain the parameter 'grant_type'
AADSTS900144: The request body must contain the following parameter: 'grant_type'.
The token endpoint did not find grant_type in the form body of your POST.
3 causes, each with a fixSecurity
Angular HttpClient does not add the X-XSRF-TOKEN header to a request
403 Forbidden on POST, PUT or DELETE while GET works
Angular copies the XSRF-TOKEN cookie into an X-XSRF-TOKEN header only for mutating requests to the same origin, and only when the cookie is readable by script.
5 causes, each with a fixSecurity
An [Authorize] endpoint rejects a request with a bearer token
Authorization Header sent: no Bearer token sent.
An [Authorize] endpoint without a policy name requires an authenticated user.
4 causes, each with a fixSecurity
ASP.NET Core API rejects an Angular user's token: the signature key was not found
401 Unauthorized
The API looked for the key named by the token's kid in the key set it downloaded from your Authority, and the key was not there.
4 causes, each with a fixSecurity
ASP.NET Core rejects the token before checking it: IDX12741, three segments expected
401 Unauthorized
IDX12741 means the string the API pulled out of the Authorization header is not shaped like a JWT: a signed token has three dot-separated parts and an encrypted one has five.
3 causes, each with a fixSecurity
A Microsoft Entra access token shows Invalid Signature when you check it yourself
Tokens that a Microsoft API receives might not always be a JWT that can be decoded
A token issued for Microsoft Graph is not meant for you to validate, so a failed signature check is expected.
2 causes, each with a fix