[Tip] github Permission denied (publickey).

Sep 21, 2023


If you experience the following problem when doing a git push or pull, it is often caused by not having a ssh key added to your ssh-agent.
If your github repository has remote set up well, you can easily solve the problem with the command below.

git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.


> alias myssh='eval "$(ssh-agent -s)" && ssh-add --apple-use-keychain {ssh-key-path}'
> myssh


> alias myssh='eval "$(ssh-agent -s)" && ssh-add --apple-use-keychain ~/.ssh/chrisjune-github'
> myssh

Agent pid 19783
Identity added: /Users/user/.ssh/chrisjune-github (chrisjune@mail.com)

Add it to your shell profile configuration file(zshrc, bachrc, bachprofile), and you can always run the command as an alias.