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 flawl
The easiest and straightforward approach (IMHO) - no config files not too much hassle
Just create another ssh key.
Let's say you have aa new work GitHub account, just create a new key for it:
sh-keygen -t rsa -C "email@work_mail.com" -f "id_rsa_work_user1"`
Now you should have the old one and the new one, to see them, run:
ls -al ~/.ssh
You need to run the above only once.
From now on, every time you want to switch between the two, just run:
ssh-add -D
ssh-add ~/.ssh/id_rsa_work_user1 #make to use this without the suffix .pub
In order the switch to the old one, run again:
ssh-add -D
ssh-add ~/.ssh/<previous id_rsa>
.gitconfig
using a personal access tokenIf you do not want to modify your host file, use SSH keys, or setup a .gitconfig
for each repo, then you may use a personal .gitconfig
that you basically include from the root level config. Given an OSX directory structure like
# root level git config
~/.gitconfig
# your personal repos under some folder like
../personal/
../home/
~/Dropbox/
Add a .gitconfig
in your personal folder, such as ~/Dropbox/.gitconfig
[user]
email = first.last@home.com
name = First Last
[credential]
username = PersonalGithubUsername
helper = osxkeychain
In your root level .gitconfig
add an includeIf section to source your personal config whenever you are in your personal directory. Any settings there will override the root config as long as the includeIf
comes after the settings you want to override.
[user]
email = first.last@work.com
name = "First Last"
[credential]
helper = osxkeychain
[includeIf "gitdir:~/Dropbox/**"]
path = ~/Dropbox/.gitconfig
Try pushing to your personal repo or pulling from your private repo
git push
# prompts for password
When prompted enter either your personal password or, better yet, your personal access token that you have created in your account developer settings. Enter that token as your password.
Assuming you are already using git-credential-osxkeychain, your personal credentials should be stored in your keychain, so two github
entries will show up, but with different accounts.
Here is a link which explains step by step how to do it and also in a very easy and self-explanatory manner.
There are mainly 4 major steps of adding multiple GitHub accounts on the same PC.
Step 1: Create SSH keys for all accounts
Step 2: Add SSH keys to SSH Agent
Step 3: Add SSH public key to the Github
Step 4: Create a Config File and Make Host Entries
The explained steps can be found here
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 and
2) 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:
cd ~/.ssh
ssh-keygen -t rsa -C "email@work.com" -f "id_rsa_WORK"
ssh-keygen -t rsa -C "email@gmail.com" -f "id_rsa_PERSONAL"
As a result of above commands, id_rsa_WORK
and id_rsa_WORK.pub
files will be created for your work account (ex - git.work.com) and id_rsa_PERSONAL
and id_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.
If you have created or cloned another repository and you were not able to pull from origin
or upstream
adding the ssh key at that directory using the following command worked.
This is the error I was getting here:
Warning: Permanently added the RSA host key for IP address '61:fd9b::8c52:7203' to the list of known hosts.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I used the following command, this works:
ssh-add ~/.ssh/id_rsa_YOUR_COMPANY_NAME
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:
$ touch ~/.ssh/config
Then edit that file to be something like this (one entry per account):
#Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
Host github-COMPANY
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_COMPANY