Fix issue: keys won’t load automatically to ssh-agent from ssh config file

First of all AddKeysToAgent option will only load keys from config file on first use. So don’t expect to see identities in the agent list before that, e.g. after shell login. If you want to test “first use” you can clear all identities from agent using ssh-add -D, and then run some command that will load the keys, for example ssh -T git@github.com.

Now, git pull and git push will also try to automatically load identities from config file. But it will only succeed, if it finds the record in the file. In my case the issue was that I used HostName, like this

Host nezhyborets-Bitbucket
    HostName bitbucket.org
    ...

Host nezhyborets-Github
    HostName github.com
    ...

In this case, agent wouldn’t find the record, because it would (naturally) look for bitbucket.org, and wouldn’t find it, because in such configuration host is hidden under alias (that git knows nothing about). Changing the file to following did the trick:

Host bitbucket.org
    ...

Host github.com
    ...

Now when git commands like pull or push would ask agent for keys, it would successfully load them automatically.

Leave a comment