push using multiple account / multiple identity on github / bitbucket
Asked Answered
F

3

6

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!

Feign answered 10/9, 2013 at 17:9 Comment(1)
Cleaned up now - you may want to remove that commentSynapse
P
6

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!

Pogonia answered 10/9, 2013 at 17:13 Comment(1)
This blog post explains how to use the configuration described above nicely: dbushell.com/2013/01/27/multiple-accounts-and-ssh-keysOrthochromatic
P
4

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 = [email protected]: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
Patterson answered 10/9, 2013 at 20:16 Comment(0)
M
3

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 = [email protected]:mybitbucketusername/myworkproject.git

Change the url line to:

url = git@work:mybitbucketusername/myworkproject.git
Mudguard answered 19/3, 2014 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.