push using multiple account / multiple identity on github / bitbucket

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 05:25:26

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!

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!