Let suppose I have two github accounts, https://github.com/rahul-office and https://github.com/rahul-personal. Now i want to setup my mac to easily talk to both the github accounts.
NOTE: This logic can be extended to more than two accounts also. :)
The setup can be done in 5 easy steps:
- 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
- Step 5 : Cloning GitHub repositories using different accounts
First make sure your current directory is your .ssh folder.
$ cd ~/.ssh
Syntax for generating unique ssh key for ann account is:
ssh-keygen -t rsa -C "your-email-address" -f "github-username"
here,
-C stands for comment to help identify your ssh key
-f stands for the file name where your ssh key get saved
ssh-keygen -t rsa -C "olayasturias_github_email@gmail.com" -f "olayasturias"
ssh-keygen -t rsa -C "otheruser_email@gmail.com" -f "otheruser"
Notice here olayasturias and otheruser are the username of two separate github accounts
After entering the command the terminal will ask for passphrase, leave it empty and proceed.
Now after adding keys , in your .ssh folder, a public key and a private will get generated.
The public key will have an extention .pub and private key will be there without any extention both having same name which you have passed after -f option in the above command. (in my case github-rahul-office and github-rahu-personal)
Now we have the keys but it cannot be used until we add them to the SSH Agent.
ssh-add -K ~/.ssh/olayasturias
ssh-add -K ~/.ssh/otheruser
You can read more about adding keys to SSH Agent here.
For the next step we need to add our public key (that we have generated in our previous step) and add it to corresponding github accounts.
For doing this we need to:
1. Copy the public key
We can copy the public key either by outputing the github-rahul-office.pub file in cat and then copying the content of it.
cat ~/.ssh/olayasturias.pub
cat ~/.ssh/otheruser.pub
OR
We can directly copy the content of the public key file in the clipboard.
pbcopy < ~/.ssh/olayasturias.pub
pbcopy < ~/.ssh/otheruser.pub
2. Paste the public key on Github
- Sign in to Github Account
- Goto Settings > SSH and GPG keys > New SSH Key
- Paste your copied public key and give it a Title of your choice.
OR
- Sign in to Github
- Paste this link in your browser (https://github.com/settings/keys) or click here
- Click on New SSH Key and paste your copied key.
The ~/.ssh/config file allows us specify many config options for SSH.
If config file not already exists then create one (make sure you are in ~/.ssh directory)
touch config
The commands below opens config in your default editor....Likely TextEdit, VS Code.
gedit config
Now we need to add these lines to the file, each block corresponding to each account we created earlier.
#olayasturias account
Host github.com-olayasturias
HostName github.com
User git
IdentityFile ~/.ssh/olayasturias
#othermember account
Host github.com-othermember
HostName github.com
User git
IdentityFile ~/.ssh/othermember
So we are done with our setups and now its time to see it in action. We will clone a repository using one of the account we have added.
Make a new project folder where you want to clone your repository and go to that directory from your terminal.
For Example: I am making a repository on my personal github account and naming it TestRepo Now for cloning the repo use the below command:
git clone git@github.com-{your-username}:{owner-user-name}/{the-repo-name}.git
[e.g.] git clone ggit clone git@github.com:olayasturias/my_secret_repo_youll_never_get.git
From now on, to ensure that our commits and pushes from each repository on the system uses the correct GitHub user — we will have to configure user.email and user.name in every repository freshly cloned or existing before.
To do this use the following commands.
git config user.email "freyja@gmail.com"
git config user.name "Freyja WS"
git config user.email "odin@gmail.com"
git config user.name "Odin WS"
Pick the correct pair for your repository accordingly.
To push or pull to the correct account we need to add the remote origin to the project
git remote add origin git@github.com:olayasturias
git remote add origin git@github.com:otheruser
Now you can use:
git push
git pull