Using multiple Github accounts with SSH

When I started using Github, I used Github Desktop for sometime because that felt easy. Down the line, I learnt using git via CLI. And, I haven't touched the electron tool back ever. But, I was still using Github via HTTP vs SSH.
I fell in love with ssh when I first discovered it. Once setup you can use ssh to work with multiple repositories under same account. The only downside is the initial setup that might feel complicated if you are using it for the very first time. However, it saves you from the pain of password/token based authentication (...painful).
If you haven't used ssh before, these two articles would help you set up pretty quick.
So far so good, this works good for a single account. What if you have to use multiple accounts? Personal and Work?
- Use
ssh-keygento start creating your new ssh key. Note, this email should be same as the one you use with your Github account.ssh-keygen -t rsa -C "youremail@domain.com" Give this ssh-key a different name, so that it does not conflict/overwrite existing keys. All existing
sshkeys can be found under~/.sshfolder. I named my own as "pavittarxnoauth".Enter file in which to save the key (/home/pavittarx/.ssh/id_rsa): pavittarxnoauthIn the next steps, provide a password or simply hit enter if you do not want to add any passwords while authentication.
Check if the key has been successfully created. If you cannot find the key in .ssh folder. Repeat steps 1-3.
ls ~/.sshStart your ssh-agent if you have not already.
eval `ssh-agent -s`Add the key to your ssh agent.
ssh-add ~/.ssh/pavittarxnoauthNavigate to
~/.sshdirectory.cd ~/.sshAdd following entry to the
configfile. You can also create one if it doesn't exist. Replace, pavittarxnoauth with your key-file name. Also, replacegithub-noauthto anything as per your preference.Host github-noauth HostName github.com User git IdentityFile ~/.ssh/pavittarxnoauthAdd the
~/.ssh/<yourkeyfilename.pub>to your Github account as stated here.Clone your repo with ssh, or change the
git remoteandgit configfor the repository.git clone git@github-noauth:<your-github-username>/<your-git-repo>For existing repository, that has been cloned already.
- Change
git remote origin
git remote remove origin git remote add origin git@github-noauth:<github-username>/<git-repo>- Change
git config
git config --local user.email "your-github-email@domain.com" git config --local user.name "github-username"- Change
You can use same process to add multiple keys, with same or different accounts to Github. Also, the same is reproducable for other git providers such as Bitbucket/ Gitlab.





