If you're using Mac OS (High Sierra or otherwise) you might have seen this error when you tried to push git code (to GitHub), especially if you tried doing so from something such as Visual Studio Code (vscode):
> git push email@example.com: Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
Thankfully, the solution is extremely simple. All you have to do is create (or edit) a ~/.ssh/config file and add the following:
Host * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa
And then run your SSH command(s) again. It also helps if you're using ssh-agent.
Why the error happens in the first place?
For whatever reason, at least in my case, VSCode wasn't able to detect and use the correct SSH details. So instead of using my proper Github login details that I was using via SSH keys and passphrases and all, it was trying to use the generic default “firstname.lastname@example.org” login. Well of course that's not going to be able to push nor sync or anything for a private repo!
The solution basically says that for any (*) hosts add the keys to ssh agent, use the MacOS keychain (so that it's all globally available especially to picky applications) and use this (~/.ssh/id_rsa) specific keyfile (which is locked behind a passphrase for good measure).
If this worked for you, let us know. If it didn't work for you, also let us know and let's see if we can find a solution that does.