Trying to work on my both my actual "work" repos, and my personal repos on git hub, from my computer.
The work account was set up first, and everything works flawlessly.
My personal account, however cannot seem to push to my personal repo, which is set up under a different account/email.
I've tried copying my work key up to my personal account, but that throws an error, because of course a key can be only attached to one account.
How can I push/pull to and from both accounts, from their respective github credentials?
The details at http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/ linked to by mishaba work very well for me.
From that page:
Then edit that file to be something like this (one entry per account):
This answer is for beginners (none-git gurus). I recently had this problem and maybe its just me but most of the answers seemed to require rather advance understanding of git. After reading several stack overflow answers including this thread, here are the steps I needed to take in order to easily switch between GitHub accounts (e.g. assume two GitHub accounts, github.com/personal and gitHub.com/work):
ls -al ~/.ssh
files with extension
.pub
are your ssh keys so you should have two for thepersonal
andwork
accounts. If there is only one or none, its time to generate other wise skip this.- Generating ssh key: login to github (either the personal or work acc.), navigate to Settings and copy the associated email.
now go back to Terminal and run
ssh-keygen -t rsa -C "the copied email"
, you'll see:Generating public/private rsa key pair.
Enter file in which to save the key (/.../.ssh/id_rsa):
id_rsa is the default name for the soon to be generated ssh key so copy the path and rename the default, e.g.
/.../.ssh/id_rsa_work
if generating for work account. provide a password or just enter to ignore and, you'll read something like The key's randomart image is: and the image. done.Repeat this step once more for your second github account. Make sure you use the right email address and a different ssh key name (e.g. id_rsa_personal) to avoid overwriting.
At this stage, you should see two ssh keys when running
ls -al ~/.ssh
again.pbcopy < ~/.ssh/id_rsa_work.pub
, replaceid_rsa_work.pub
with what you called yours.Now that our ssh key is copied to clipboard, go back to github account [Make sure you're logged in to work account if the ssh key you copied is
id_rsa_work
] and navigate toSettings - SSH and GPG Keys and click on New SSH key button (not New GPG key btw :D)
give some title for this key, paste the key and click on Add SSH key. You've now either successfully added the ssh key or noticed it has been there all along which is fine (or you got an error because you selected New GPG key instead of New SSH key :D).
Edit the global git configuration: Last step is to make sure the global configuration file is aware of all github accounts (so to say).
Run
git config --global --edit
to edit this global file, if this opens vim and you don't know how to use it, pressi
to enter Insert mode, edit the file as below, and press esc followed by:wq
to exit insert mode:[inside this square brackets give a name to the followed acc.] name = github_username email = github_emailaddress [any other name] name = github_username email = github_email [credential] helper = osxkeychain useHttpPath = true
Done!, now when trying to push or pull from a repo, you'll be asked which GitHub account should be linked with this repo and its asked only once, the local configuration will remember this link and not the global configuration so you can work on different repos that are linked with different accounts without having to edit global configuration each time.
Unlike other answers, where you need to follow few steps to use two different github account from same machine, for me it worked in two steps.
You just need to :
1) generate SSH public and private key pair for each of your account under
~/.ssh
location with different names and2) add the generated public keys to the respective account under
Settings
>>SSH and GPG keys
>>New SSH Key
.To generate the SSH public and private key pairs use following command:
As a result of above commands,
id_rsa_WORK
andid_rsa_WORK.pub
files will be created for your work account (ex - git.work.com) andid_rsa_PERSONAL
andid_rsa_PERSONAL.pub
will be created for your personal account (ex - github.com).Once created, copy the content from each public (
*.pub
) file and do Step 2 for the each account.PS : Its not necessary to make an host entry for each git account under
~/.ssh/config
file as mentioned in other answers, if hostname of your two accounts are different.I use shell scripts to switch me to whatever account I want to be "active". Essentially you start from a fresh start, get one account configured properly and working, then move the these files to a name with the proper prefix. From then on you can use the command "github", or "gitxyz" to switch:
I've had great luck with this. I also created a run script in Xcode (for you Mac users) so it would not build my project unless I had the proper setting (since its using git):
Run Script placed after Dependencies (using /bin/ksh as the shell):
EDIT: added tests for new files existence and copying old files to /tmp to address comment by @naomik below.
another easier way is using multiple desktop apps, like what i am doing, using account A on Github desktop, while using account B on Github Kraken
Thanks user1107028:
I found this the best way when using multiple github accounts. I cant use only one account, its just not possible, and I couldnt get SSH to behave no matter what i did. Id have voted up the answer but i dont have enough points - I wanted to acknowledge this simple workaround for those like me who need a clean easy solution. It worked first time and saved me hours more struggling.