Search code examples
githubssh-keysgit-push

Specifying non-default SSH key during git push


I have several SSH keys:

~/.ssh/
    id_rsa
    id_rsa.pub
    fizz
    fizz.pub
    buzz
    buzz.pub

I cloned a GitHub project using HTTPS (e.g. git clone https://github.com/someorg/somerepo.git). I made some changes and now I'm trying to push them, however I'm getting the famous error:

"Permission to someorg/somerepo.git denied to blah-user"

Where blah-user is (I believe) the user associated with my id_rsa SSH key. So I think git is defaulting to use my id_rsa key, whereas I would like to specifically user the buzz key for making the push (I've associated that buzz key with my GitHub account).

Any ideas as to what I could do here, or what I can do to specifically use the buzz key as part of my push?


Solution

  • Since you cloned the https url git will try to use https when pushing to your remote. HTTP(S) and SSH are different protocols and use different credentials so it will in fact not look at your ssh-keys at all.

    Git will probably ask for your http-credentials on the prompt or you can use a 'git-credential-store' helper to keep them on file https://git-scm.com/docs/git-credential-store

    If you clone using the ssh protocol git will delegate the authentication to SSH which will look for a private key in the .ssh directory.

    You can define which key in many ways, I prefer to set up a .ssh/config file with an entry for the host I'm connecting to.

    Host thehost.com
        Hostname thehost.com
        IdentityFile ~/.ssh/my_github_key_id_rsa
        User mu-username