On Mac, I had the same issue as the OP, and I had multiple identities set up and when I was running git push
(even with no local changes) I saw an error message:
$ git push
ERROR: Write access to repository not granted. fatal: Could not read from remote repository.
Turns out, the issues that I had were twofold:
- When I ran
ssh-add -l
, I could see there was an extraneous identity - i.e. one which didn't exist anymore. That is, I thought I deleted it earlier, but it was still registered on ssh-agent
.
- I had too many running
ssh-agent
processes (normally there should only be 1-2 running) when I ran pgrep ssh-agent
.
The fix it, the solution was rather simple. First, it is helpful to authorize the SSH key on GitHub.
If using Enterprise Cloud, you might need to
authorize the SSH key for use with SAML.
Under Settings > SSH and GPG keys on your target GitHub account, find your SSH key and ensure SSO is enabled.
Choose Configure SSO
and Authorize
- see image below.
Next, to be safe I closed most if not all open terminal windows.
Then to kill all those running ssh-agent
processes, I ran:
kill $(pgrep ssh-agent)
Then, I started a new window and when it said:
$ ssh-add -l
The agent has no identities
If SSH_AUTH_SOCK
is unset, you might need to first start up ssh-agent
. Note tthat I didn't need to run this command myself.
test -z "$SSH_AUTH_SOCK" && eval "$(ssh-agent -s)"
I just add them back with ssh-add
:
ssh-add ~/.ssh/id_ed25519_<user>
I can confirm that ssh-add -l
now lists all identities after restarting a new terminal window.
If anyone still runs into issues, there could be a problem with the clone or push URL for a project. For example, using HTTPS instead of SSH.
Note that insteadOf
in git config does not chain. This post (along with its answer) might be helpful.
$SSH_AGENT_PID
. – Forresterkill -9 $PID_SSH_AGENT
– Antonetteantonissh-agent my-script
to start an agent that exits as soon asmy-script
exits. – Gonsalezps aux | grep ssh-agent
. If you pipe this further to| wc -l
and this equals "0", there are no running processes anymore. – Pammie