Running multiple git ssh logins via Terminal

gitsshterminal

I recently set up an SSH key for by Bitbucket account, which works very nicely. But I also have other git repos like Github, GitLab (non ssh at the moment) and I came to push to one of those other accounts and I think the Terminal defaulted to use my bitbucket ssh keys.

So how would I force Terminal to either let me login with a username and password when using a github repo, or let me pick which ssh keys to use, say I had setup up keys for Gitlab and Github?

Best Answer

There a different ways of achieving this.

  1. you could use the same key for all your git repos.

  2. you could use different keys and specify which one to use when logging into a remote system:

    $ ssh -i ~/.ssh/bitbucket-key user@host
    $ ssh -i ~/.ssh/gitlab-key user@host
    

-i identity_file

Selects a file from which the identity (private key) for public key authentication is read. The default is ~/.ssh/id_dsa, ~/.ssh/id_ecdsa, ~/.ssh/id_ed25519 and ~/.ssh/id_rsa. Identity files may also be specified on a per-host basis in the configuration file. It is possible to have multiple -i options (and multiple identities specified in configuration files). If no certificates have been explicitly specified by the CertificateFile directive, ssh will also try to load certificate information from the filename obtained by appending -cert.pub to identity filenames.

I would create a key pair for each git repo, then in Bitbucket you would upload the key to the Bitbucket website. Gitlab and Github work the same way.

Once you uploaded your key to your repository it is enough to git push without need to specify your key.