The only way I've succeeded so far is to employ SSH.
Navigate to ~/.ssh and open up the config file. By default, it should look something like this:
Host * AddKeysToAgent yes UseKeyChain yes IdentityFile ~/.ssh/id_rsa ForwardAgent yes
Assuming you've got 2 github accounts, for work and play, lets get your Mac to "register" them. To do that that you'll need to create SSH key pairs for each account. If you have already setup your Mac to SSH with one of them, continue on with the following for the second account.
- Creating the SSH keys. For each SSH key pairs:
-
run
ssh-keygen -t rsa -b 4096 -C "your_email@example.com" -
You'll be prompted: "Enter a file in which to save the key" and the suggested default filename would be
id_rsa. This filename will be used for your SSH private and public keys so remember to make it unique, eg.user-1. This step will generate both the private and public keys,user-1anduser-1.pubrespectively.
-
Register the contents of the public key, e.g
user-1.pub, into the respective GitHub accounts. Follow the steps here to do so -
Head back over to the SSH
configfile at~/.sshand amend accordingly to:
#user1 account Host github.com-user1 HostName github.com User git IdentityFile ~/.ssh/github-user1 IdentitiesOnly yes #user2 account Host github.com-user2 HostName github.com User git IdentityFile ~/.ssh/github-user2 IdentitiesOnly yesReplace
user1oruser2with your GitHub usernames
- Go ahead to git clone the respective GitHub account
git clone git@github.com-user1:user1/your-repo-name.git your-repo-name_user1
- Configure your git identity:
- open up local git config
git config --local -eand add:
[user]
name = user1
email = user1@gmail.com- Ensure your remote url is in the format of
git@github.com-user1:user1/your-repo-name.git your-repo-name_user1
Now you can git push and pull all you like!