Question

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!

Was it helpful?

Solution

You can use ~/.ssh/config as explained here:

https://confluence.atlassian.com/pages/viewpage.action?pageId=271943168

Host workdid
 HostName bitbucket.org
 IdentityFile ~/.ssh/workdid
Host personalid
 HostName bitbucket.org
 IdentityFile ~/.ssh/personalid

Otherwise, if you just want to "switch account" before doing the pushing, you can use ssh-add. Open the Terminal.app, run ssh-agent and run ssh-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!

OTHER TIPS

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

[remote "origin"]
    url = git@github.org:account1/repository.git 
    #This one uses the default ssh keys

[remote "account2"]
    url = https://github.com/account2/repository.git 
    #This will need password while pushing/pulling
[remote "account3"]
    url = https://github.com/account3/repository.git 
    #This will need password while pushing/pulling

Then for normal operation, you can push/pull using ssh keys

git pull origin branch_name
git push origin branch_name

And for pushing to the other account repos, you can push via https with password

git push account2 branch_name
git push account3 branch_name

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:

# ~/.ssh/config
Host work
  HostName bitbucket.org
  IdentityFile ~/.ssh/work

Host personal
  HostName bitbucket.org
  IdentityFile ~/.ssh/personal

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:

# ~/repos/myworkproject/.git/config
# or you can access this file by going into the repo and running `git config -e`
...snip...
[remote "origin"]
  fetch = ...
  url = git@bitbucket.org:mybitbucketusername/myworkproject.git

Change the url line to:

url = git@work:mybitbucketusername/myworkproject.git
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top