Fixing Git:git@gitlab.com: Permission denied(publicly) with VSCODE on macOS


Recently, I switched my gitlab repository from https authorization to ssh authorization. When I was generating the ssh key. I set a passphrase. And with the newer version of macOS, on default the ssh agent might not be able to auto remember this passphrase which could lead to an error when using the integrated version of Git in VSCODE.

To solve this, first we need to check whether the ssh agent is getting the identity correctly.

ssh-add -l

If the terminal returns The agent has no identities. , we can use the below command to add the ssh key to the ssh agent:

ssh-add -K ~/.ssh/[name of the ssh key]

And if it’s still not working after the identity is added to the ssh agent. This might due to a macOS update that disable to auto-remember passphrase for those ssh keys. See below quote:

If you’re using macOS Sierra 10.12.2 or later, you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain.

https://docs.github.com/en/enterprise-cloud@latest/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

To solve this, we can just create a config file under the ~/.ssh folder if there is none.

In the ~/.ssh/config, we can add the below setting:

Host *
  UseKeychain yes
  AddKeysToAgent yes
  IdentityFile ~/.ssh/[name of the ssh key]

After that, just restart the VSCODE, and the integrated git should work as expected.

,

Leave a Reply

Your email address will not be published. Required fields are marked *