Avoid SSH passphrase for Git


Problem

I was denied access after running the Git command on a repository I owned:

git pull
fatal: Could not read from remote repository.

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

This turned out to be an SSH issue so I took the following steps to fix it.

Check SSH key

First, I checked if I had a valid SSH key pair:

ls -a ~/.ssh/

I didn’t see the following:

id_ed25519
id_ed25519.pub

So I had to generate a new SSH key.

Generate SSH key

To generate an SSH key:

ssh-keygen -t ed25519 -C "your_email@example.com"

Add SSH key to agent

Start the ssh-agent if it’s not running already:

eval $(ssh-agent -s)
Agent pid 5451

The ssh-agent manages your keys so check if it’s there:

ssh-add -l
The agent has no identities.

Since there are no keys, add your key to the ssh-agent (hit Enter when asked to enter a passphrase):

ssh-add --apple-use-keychain ~/.ssh/id_ed25519
Identity added: /Users/remarkablemark/.ssh/id_ed25519 (your_email@example.com)

Then add your new SSH key to your GitHub account.

Now you should be able to perform SSH operations without being asked for a passphrase:

git pull
Already up to date.

SSH config

To prevent having to enter a passphrase after restart, add the following to your SSH config file ~/.ssh/config:

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

Or run this command:

cat > ~/.ssh/config << EOF
Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519
EOF

Check SSH connection

Check your SSH connection with GitHub:

ssh -vT git@github.com

You should see:

Hi remarkablemark! You've successfully authenticated, but GitHub does not provide shell access.


Please support this site and join our Discord!