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?
I found this gem to be very useful: sshwitch
https://github.com/agush22/sshwitch
http://rubygems.org/gems/sshwitch
It helps to switch out ssh keys. Remember to back up everything first!
Also to make sure that commits have the correct email address associated with them, I made sure that the ~/.gitconfig file had the proper email address.
All you need to do is configure your SSH setup with multiple SSH keypairs.
This link is easy to follow (Thanks Eric): http://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574
Generating SSH keys (Win/msysgit) https://help.github.com/articles/generating-an-ssh-key/
Also, if you're working with multiple repositories using different personas, you need to make sure that your individual repositories have the user settings overridden accordingly:
Setting user name, email and GitHub token – Overriding settings for individual repos https://help.github.com/articles/setting-your-commit-email-address-in-git/
Hope this helps.
Note: Some of you may require different emails to be used for different repositories, from git 2.13 you can set the email on a directory basis by editing the global config file found at:
~/.gitconfig
using conditionals like so:And then your work specific config ~/work/.gitconfig would look like this:
Thank you @alexg for informing me of this in the comments.
Getting into shape
To manage a git repo under a separate github/bitbucket/whatever account, you simply need to generate a new SSH key.
But before we can start pushing/pulling repos with your second identity, we gotta get you into shape – Let's assume your system is setup with a typical
id_rsa
andid_rsa.pub
key pair. Right now yourtree ~/.ssh
looks like thisFirst, name that key pair – adding a descriptive name will help you remember which key is used for which user/remote
Next, let's generate a new key pair – here I'll name the new key
github-otheruser
Now, when we look at
tree ~/.ssh
we seeNext, we need to setup a
~/.ssh/config
file that will define our key configurations. We'll create it with the proper owner-read/write-only permissionsOpen that with your favourite editor, and add the following contents
Presumably, you'll have some existing repos associated with your primary github identity. For that reason, the "default" github.com
Host
is setup to use yourmainuser
key. If you don't want to favour one account over another, I'll show you how to update existing repos on your system to use an updated ssh configuration.Add your new SSH key to github
Head over to github.com/settings/keys to add your new public key
You can get the public key contents using: copy/paste it to github
Now your new user identity is all setup – below we'll show you how to use it.
Getting stuff done: cloning a repo
So how does this come together to work with git and github? Well because you can't have a chicken without and egg, we'll look at cloning an existing repo. This situation might apply to you if you have a new github account for your workplace and you were added to a company project.
Let's say
github.com/someorg/somerepo
already exists and you were added to it – cloning is as easy asThat bolded portion must match the
Host
name we setup in your~/.ssh/config
file. That correctly connects git to the correspondingIdentityFile
and properly authenticates you with githubGetting stuff done: creating a new repo
Well because you can't have a chicken without and egg, we'll look at publishing a new repo on your secondary account. This situation applies to users that are create new content using their secondary github account.
Let's assume you've already done a little work locally and you're now ready to push to github. You can follow along with me if you'd like
Now configure this repo to use your identity
Now make your first commit
Check the commit to see your new identity was used using git log
Alright, time to push to github! Since github doesn't know about our new repo yet, first go to github.com/new and create your new repo – name it somerepo
Now, to configure your repo to "talk" to github using the correct identity/credentials, we have add a remote. Assuming your github username for your new account is
someuser
...That bolded portion is absolutely critical and it must match the
Host
that we defined in your~/.ssh/config
fileLastly, push the repo
Update an existing repo to use a new SSH configuration
Say you already have some repo cloned, but now you want to use a new SSH configuration. In the example above, we kept your existing repos in tact by assigning your previous
id_rsa
/id_rsa.pub
key pair toHost github.com
in your SSH config file. There's nothing wrong with this, but I have at least 5 github configurations now and I don't like thinking of one of them as the "default" configuration – I'd rather be explicit about each one.Before we had this
So we will now update that to this (changes in bold)
But that means that now any existing repo with a
github.com
remote will no longer work with this identity file. But don't worry, it's a simple fix.To update any existing repo to use your new SSH configuration, simply open the repo's git config file and update the url!
Update the remote origin field (changes in bold)
That's it. Now you can
push
/pull
to your heart's contentSSH key file permissions
If you're running into trouble with your public keys not working correctly, SSH is quite strict on the file permissions allowed on your
~/.ssh
directory and corresponding key filesAs a rule of thumb, any directories should be
700
and any files should be600
- this means they are owner-read/write-only – no other group/user can read/write themHow I manage my SSH keys
I manage separate SSH keys for every host I connect to, such that if any one key is ever compromised, I don't have to update keys on every other place I've used that key. This is like when you get that notification from Adobe that 150 million of their users' information was stolen – now you have to cancel that credit card and update every service that depends on it – what a nuisance.
Here's what my
~/.ssh
directory looks like: I have one.pem
key for each user, in a folder for each domain I connect to. I use.pem
keys to so I only need one file per key.And here's my corresponding
/.ssh/config
file – obviously the github stuff is relevant to answering this question about github, but this answer aims to equip you with the knowledge to manage your ssh identities on any number of services/machines.Getting your SSH public key from a PEM key
Above you noticed that I only have one file for each key. When I need to provide a public key, I simply generate it as needed.
So when github asks for your ssh public key, run this command to output the public key to stdout – copy/paste where needed
Note, this is also the same process I use for adding my key to any remote machine. The
ssh-rsa AAAA...
value is copied to the remote's~/.ssh/authorized_keys
fileConverting your
id_rsa
/id_rsa.pub
key pairs to PEM formatSo you want to tame you key files and cut down on some file system cruft? Converting your key pair to a single PEM is easy
Or, following along with our examples above, we renamed
id_rsa -> github-mainuser
andid_rsa.pub -> github-mainuser.pub
– soNow just to make sure that we've converted this correct, you will want to verify that the generated public key matches your old public key
Now that you have your
github-mainuser.pem
file, you can safely delete your oldgithub-mainuser
andgithub-mainuser.pub
files – only the PEM file is necessary; just generate the public key whenever you need it ^_^Creating PEM keys from scratch
You don't need to create the private/public key pair and then convert to a single PEM key. You can create the PEM key directly.
Let's create a
newuser.pem
Getting the SSH public key is the same
Simpler and Easy fix to avoid confusions..
For Windows users to use multiple or different git accounts for different projects.
Following steps: Go Control Panel and Search for Credential Manager. Then Go to Credential Manager -> Windows Credentials
Now remove the git:https//github.com node under Generic Credentials Heading
This will remove the current credentials. Now you can add any project through git pull it will ask for username and password.
When you face any issue with other account do the same process.
Thanks
refer to image
You do not have to maintain two different accounts for personal and work. In fact, Github Recommends you maintain a single account and helps you merge both.
Follow the below link to merge if you decide there is no need to maintain multiple accounts.
https://help.github.com/articles/merging-multiple-user-accounts/
By creating different host aliases to github.com in your ~/.ssh/config, and giving each host alias its own ssh key, you can easily use multiple github accounts without confusion. That’s because github.com distinguishes not by user, which is always just git, but by the ssh key you used to connect. Just configure your remote origins using your own host aliases.”
The above summary is courtesy of comments on the blog post below.
I've found this explanation the clearest. And it works for me, at least as of April 2012.
http://net.tutsplus.com/tutorials/tools-and-tips/how-to-work-with-github-and-multiple-accounts/