Why Does Git Keep Asking for My SSH Password (Bitbucket / Github)?

I’m making an effort, when I learn something while resolving an issue, to document the process. I’d rather not have to muddle through the haze of déjà vu trying to solve the same problem a second and third time. The writing both helps me learn and serves as a resource when I forget. This is one of those “documentation” posts; hopefully others find it helpful as well.

I recently updated to MacOS Sierra (10.12.2). After the update, git began prompting me for my ssh password on every interaction with the remote repo:

Enter passphrase for key '/Users/matthew/.ssh/id_rsa':

I started looking around for a solution online, and initially most of what I found had to do with people cloning repos via https, instead of git, which was not applicable.

Finally, this Stack Overflow post pointed me in the right direction - I needed to make sure the SSH agent was running, and that my key was added to it. Spoiler - I just needed to re-add my key to the SSH agent to resolve the issue.

I ran the following command (after reading Github’s guide on SSH agent forwarding) to check if my key was available to the agent:

$ ssh-add -L
The agent has no identities.

This was obviously the issue; somehow my key had been forgotten. The guide goes on to explain that:

On Mac OS X, ssh-agent will “forget” this key, once it gets restarted during reboots.

Frustrating, but easy enough to resolve. Because I couldn’t remember how to add the key, I tracked down the Github guide on setting up SSH keys. First I had to make sure the agent was running:

$ eval "$(ssh-agent -s)"
Agent pid 7964

Then I added my key:

$ ssh-add ~/.ssh/id_rsa

And the issue was resolved. Running ssh-add -L now showed my key had been added, and after restarting iTerm, the repeated password prompts stopped.