Is It Possible to Use Multiple GitHub, GitLab, and Bitbucket Accounts at the Same Time?
What should you do when your client asks you to use their email address to create a GitHub account that should be entirely separated from your private account? Or, let’s say that you have a client that doesn’t have much available time (as always), and they give you their GitHub account credentials. Or maybe you need to access some private repository for which you cannot change permissions and invite more users. Or maybe even your client sent you the invitation using a different email then you were expecting, and then the client went on vacation.
In all those cases, you need to manage multiple GitHub accounts from the same development machine. If you are using SSH keys, there will be a problem since your keys are already connected to your personal account. You can clone the Git repository using
https URL and type the password each time you need to deploy, but this could be a problem as you need to remember all the passwords, and keep track of the changed ones.
However, there is a way to continue working with multiple accounts using multiple SSH keys.
First, you need to generate keys with the following command:
ssh-keygen -f ~/.ssh/id_rsa_my_company_account
# press enter for empty passphrase
You can find private and public keys by running the following command:
Then, you need to add the public key to the GitHub/GitLab/Bitbucket settings. You can copy and paste the output of the following command:
There is very useful command
ssh-add to help you manage your SSH keys. To list all current saved SSH keys in every X-session, or a login session
ssh-agent is started, you can execute the following command:
ssh-add -L # list all current keys
You can start a new agent by simply calling it:
ssh-agent bash -c 'ssh-add -L' # The agent has no identities.
So, you can use these two commands (
ssh-agent) to set the keys to another account.
ssh-agent bash -c 'ssh-add ~/.ssh/id_rsa_my_company_account; git clone firstname.lastname@example.org:company/repo.git'
Another way to use those specific keys for some repositories is to configure the SSH. Let’s name that repository
my_company_account_github.com (no worry, it will be translated to
github.com domain). Just create a file
~/.ssh/config with the following content:
When you want to download the repository, just use that new fake repository domain name.
git clone git@my_company_account_github.com:company/repo.git
# this will use ~/.ssh/id_rsa_my_company_account key
If you decide to change which key you are using for an already cloned repo, you can edit
.git/config or use
git remote set-url origin git@my_company_account_github.com:company/repo.git.
To see which username (
-T) and which key (
-v) SSH is using:
ssh -T git@my_company_account_github.com
ssh -v git@my_company_account_github.com
If you want, you can generate your SSH key with the password (passphrase). I do not suggest that since it will ask for the key passphrase each time you use
Please note that for some deployment systems, like Heroku, you also need to log in before pushing to their repository. Heroku stores keys in
~/.netrc, which is created when you type
heroku login. Keep in mind, once it’s set up, it stays forever. So remember that you need to sign in with
heroku login with the same account that is connected to your SSH keys to make it work.