DEV Community

Cover image for Using multiple git accounts
Abhay Srivastav
Abhay Srivastav

Posted on • Updated on

Using multiple git accounts

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 user each time we clone a new repository.

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

git config user.name "youremail@email.com"
Enter fullscreen mode Exit fullscreen mode

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 email.

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

Next edit your ssh config file

For Linux or Mac, run below command in terminal

sudo vim ~/.ssh/config
Enter fullscreen mode Exit fullscreen mode

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

C:\Users\username\.ssh\config
Enter fullscreen mode Exit fullscreen mode

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

Lets say you have 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

to

git clone git@github.com-work:Abhay07/repository-name.git
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Youtube video downloader or cutting tool : youtubecut.com
Check your IP address / Locate an IP : myips.in

Top comments (0)