The only way I've succeeded so far is to employ SSH.
Assuming you are new to this like me, first I'd like to share with you that your Mac has a SSH config
file in a .ssh
directory. The config
file is where you draw relations of your SSH keys to each GitHub (or Bitbucket) account, and all your SSH keys generated are saved into .ssh
directory by default. You can navigate to it by running cd ~/.ssh
within your terminal, open the config
file with any editor, and 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, or check if you have one, continue on with the following for the second account.
-
run
ssh-keygen -t rsa -b 4096 -C "[email protected]"
-
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
,user-2
. This step will generate both the private and public keys,user-1
+user-1.pub
,user-2
+user-2.pub
respectively. -
GitHub has this step in detail. We're not adding the keys to the ssh-agent.
- Follow these steps to do so.
#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
user1
oruser2
with your GitHub usernames/identification-handlers
git clone [email protected]:user1/your-repo-name.git your-repo-name_user1
- Open up local git config using
git config --local -e
and add:
[user] name = user1 email = [email protected]
6. Ensure your remote url is in the right format e.g: [email protected]:user1/your-repo-name.git your-repo-name_user1
- You either run
git remote set-url origin [email protected]:user1/your-repo-name.git your-repo-name_user1
- Or amend your remote ssh-url in your local git config file:
[remote "origin"] url = [email protected]:user1/your-repo-name.git ย ย ย fetch = +refs/heads/*:refs/remotes/origin/*
Now you can git actions (pull/push/fetch...etc) all you like!
Special thanks to @pbuditi for your help!
@Manishiiitbhopal23
Configure ssh for two accounts
Clone Repositorys like below
Repository1 -
git clone [email protected]:user1/your-repo-name.git your-repo-name_user1
Repository2 -
git clone [email protected]:user2/your-repo-name.git your-repo-name_user2