Skip to main content

Setup Git cli to acess multiple accounts in terminal

· 2 min read
Sumit Joshi
DevOps Engineer

Use multiple git accounts in terminal without much hustle.

I use two github account one is work(which I use daily) and second one is my personal account which I do not use that regularly. To make my life easy I decided to setup cli for multiple account and here's how I did it.

Create two hosts in ssh config file

Create two hosts for github in ~/.ssh/config file like this

Host github.com.                ## This is the account I use for work daily
IdentityFile ~/.ssh/id_rsa
HostName github.com


Host me.github.com ## My personal account
IdentityFile ~/.ssh/sumit-private
HostName github.com

So whenever I'm cloning any repo for work I just copy git ssh clone command and run it on terminal.

➜  git-repos git clone git@github.com:MyOrg/devops-scripts.git                     
Cloning into 'devops-scripts'...
remote: Enumerating objects: 909, done.
remote: Counting objects: 100% (333/333), done.
remote: Compressing objects: 100% (169/169), done.
remote: Total 909 (delta 202), reused 267 (delta 158), pack-reused 576
Receiving objects: 100% (909/909), 703.64 KiB | 681.00 KiB/s, done.
Resolving deltas: 100% (445/445), done.

And done that's it, done.

Now, to clone or work with my other/personal account I have to just do one this while cloning repo. In git clone command change git@github.com to git@me.github.com. This will point to ssh config for my personal account, So I can clone/push/merge from/to repos in my other/personal account.

➜  git-repos git clone git@me.github.com:sjsumit10/greenbook.git                      
Cloning into 'greenbook'...
remote: Enumerating objects: 36, done.
remote: Counting objects: 100% (36/36), done.
remote: Compressing objects: 100% (29/29), done.
remote: Total 36 (delta 4), reused 32 (delta 2), pack-reused 0
Receiving objects: 100% (36/36), 8.15 KiB | 8.15 MiB/s, done.
Resolving deltas: 100% (4/4), done.
➜ git-repos

And TADA, Done.