I have multiple accounts on github / bitbucket and one unique private-public key pair for each account. The problem occurs when I need to push to the repos created by the different accounts, I will almost certainly get access denied unless I am pushing into the default account (the account that I first created).
Is there a way to switch to different credentials before pushing? I am using Source Tree on Macintosh machine. I'd prefer not to rename ~/.ssh/id_rsa
manually whenever I need to push.
Thank you very much for your input!
You can use
~/.ssh/config
as explained here:https://confluence.atlassian.com/pages/viewpage.action?pageId=271943168
Otherwise, if you just want to "switch account" before doing the pushing, you can use
ssh-add
. Open theTerminal.app
, runssh-agent
and runssh-add ~/.ssh/path_to_your_account_id_rsa
, then do the push. After pushing, you can switch back to your default account by running:ssh-add ~/.ssh/id_rsa
.Hope it helps!
For BitBucket, I found the way that worked best for me was to a) add the key to my
~/.ssh/config
, and also b) change the local config of my project.For example:
And then in my project's local git config, I changed the host part of the remote URL to the appropriate host. For example, in the following file:
Change the url line to:
On a different note, if there is one default account you usually use to pull/push, and you occasionally push in changes to your other accounts, you can add a remote referring to the
https
url for other account to your.git/config
file, though that ways you will have to enter your github passwords everytime and only the default github account (corresponding to which keys are enabled) will use ssh keys.Something like the following
Then for normal operation, you can push/pull using ssh keys
And for pushing to the other account repos, you can push via
https
with password