Push to GitHub Requesting Password Despite Existing SSH Key

When attempting a git push to a GitHub repository we may get an unexpected password prompt even if we have an SSH key.
This can happen if the repo was cloned over HTTPS, which is allowed, but we cannot push over HTTPS.

The solution is to change the upstream URL of the repo to use the Git protocol over SSH.

First, show the remote being used currently (the v flag is for verbose):

$ git remote -v

Example output:

origin https://github.com/org/repo (fetch)
origin https://github.com/org/repo (push)

To be able to push using our SSH key we just need to change the push URL, but we can change the fetch URL as well.

Change the URL: replace the protocol with ssh and add git@ in front of the hostname.

To change both the fetch and push URLs:

$ git remote set-url origin ssh://git@github.com/org/repo

To change only the push URL, we can add the –push flag:

$ git remote set-url --push origin ssh://git@github.com/org/repo

Check that both have been updated:

$ git remote -v

Output:

origin git@github.com:org/repo.git (fetch)
origin git@github.com:org/repo.git (push)

Git push over SSH should now work as expected.