Suppose I have two accounts,
- https://github.com/chalokodekare
- https://github.com/ritubhandari
I would configure my system with both the accounts so that I can work on my official and personal projects both.
To do this, follow the below steps
cd ~/.ssh
ssh-keygen -t rsa -C "chalocodekare@gmail.com" -f "chalocodekare"
ssh-keygen -t rsa -C "ritubhandari@gmail.com" -f "ritubhandari"
here,
- -C is for adding a comment, it can be anything, but it will be helpful to know the email associated with your account.
- -f is to define a filename, and creating a key with a descriptive name will help you remember which key is used for which user
After entering the command the terminal will ask for passphrase, leave it empty and proceed.
Now in .ssh folder, a public and a private key will be generated. Public key will have an extension .pub and private key will be without any extension both having same name which we have passed after -f option in the above command.
We will add above generated keys to ssh-agent.
ssh-add ~/.ssh/chalocodekare
ssh-add ~/.ssh/ritubhandari
after writing ssh-add ~/.ssh/chalocodekare, hit tab button and it'll take full system path of the file.
Now we need to add our public keys to corresponding github accounts.
1. Copy the public key
cat ~/.ssh/chalocodekare.pub & copy the content
cat ~/.ssh/ritubhandari.pub & copy the content
2. Paste the public key in Github
- Do this for each key/account one by one.
- Sign in to corresponding Github account.
- Go to Settings > SSH and GPG keys > New SSH Key
- Paste respective copied public key content. Title can be of your choice.
In .ssh folder, look for config file. If config file doesn't exist then create one (make sure to be in ~/.ssh directory)
touch config (create this file however you want)
open config (open it in edit mode however you want)
Add below lines in config file.
Host chalocodekare
HostName github.com
User git
IdentityFile ~/.ssh/chalocodekare
Host ritubhandari
HostName github.com
User git
IdentityFile ~/.ssh/ritubhandari
Now to clone https://github.com/chalokodekare/work-with-multiple-github-accounts.git, we can use any account:
git clone {your-host-in-config-file}:{owner-user-name}/{the-repo-name}.git
[e.g.] git clone chalocodekare:chalokodekare/work-with-multiple-github-accounts.git
or git clone ritubhandari:chalokodekare/work-with-multiple-github-accounts.git
To make sure our commits in each repository uses the correct GitHub user — we will have to configure user.email and user.name in all existing or newly cloned repositories.
git config user.email "chalocodekare@gmail.com"
git config user.name "Chalo Code Kare"
or
git config user.email "ritubhndari@gmail.com"
git config user.name "Ritu Bhandari"
depending on which account you're using.
Pick the correct pair for your repository accordingly.
You can verify global user name and email id using below commands.
git config --global user.email
git config --global user.name
Now you can use:
git push
git pull
To verify if your remote is pointing correctly, you can do:
git remote -v
remove & add correct remote if incorrect.
To remove existing remote
git remote remove origin
To add new remote
git remote add origin chalocodekare:chalokodekare/work-with-multiple-github-accounts.git