How to configure multiple Git accounts in your computer

Abhay Srivastav
2 min readSep 11, 2020

If you are a developer, then you must be using git. And there are chances that you might have faced the problem of using different git accounts for each git repository. How do we switch users each time we clone a new repository.

One of the solutions is to change the repository username config using the below command

git config user.name "youremail@email.com"

But you have to do this for every new repository. Fortunately, there’s a better solution of setting multiple accounts in ssh config. Below are the steps to do that.

First of all, you need to generate ssh keys for each of the emails.

How to generate and link public ssh keys to your git accounts

Next, edit your ssh config file

For Linux or Mac, run the below command in terminal

sudo vim ~/.ssh/config

For Windows, ssh config file is found generally at below location

C:\Users\username\.ssh\config

Open this folder and edit config file in any editor (Notepad/Sublime)

Lets say you have two account on github, one is your personal account and the other is work.

And from the first step, you have generated two ssh keys your-ssh-public-id using personal email and your-work-ssh-public-id using work email.

Paste below configuration in config file

Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/your-ssh-public-id
Host github.com-work
HostName github.com
User git
IdentityFile ~/.ssh/your-work-ssh-public-id

Now whenever you clone any repository, edit the clone url accordingly.
For example, if you are cloning a repository from github, then change

git clone git@github.com:Abhay07/repository-name.git

to

git clone git@github.com-work:Abhay07/repository-name.git

If you don’t change the clone url, your your-ssh-public-id will be used which uses personal email id.

You can also update the remote url after cloning the repo, with below command

git remote origin set-url git@github.com-work:Abhay07/repository-name.git

That’s it. Let me know if it helped or you have any suggestions.

P.S. — I have created some tools. Do check it out

Here is another article i have written: https://medium.com/@abhaysrivastav/dare-to-take-the-red-pill-2054ab21ec49

Tip: Share your reusable components between projects using Bit (Github). Bit makes it simple to share, document, and organize independent components from any project.

Use it to maximize code reuse, collaborate on independent components, and build apps that scale.

Bit supports Node, TypeScript, React, Vue, Angular, and more.

Example: exploring reusable React components shared on Bit.dev

--

--