Multiple git account to maintain github bitbucket .. etc with different account from a single machine
Tags: git, github, bitbucket
Published: 2022-11-22 12:11:34

Sometimes we need to config our local machine to make contribution on open source project as usual to work on our professional workspace also. It also happened that you are tightly coupled to use your official email account which is bind with your official repo.
On such a moment very often we need to maintain your personal repo or whatever you say. To contribute on this repo, every time you need to switch and config your git for every workflow. That's not good enough. So we need a optimal solution to maintain this obstacle since it is not an obstacle. Git also provide a way to maintain this. To understand this we need to deep drive in git mechanism.

But we have not time to learn git first on large scale to do this things at this first running world. :) 

Let's follow the steps:

1. You need to generate different SSH key for each an every account. Your each SSH key is the identity of your account. To generate SSH key : 

```
ssh-keygen -t ed25519 -C "your_email@example.com"
```
Let we are generate two account for this time one for user_one and another is user_two. Since for each ssh-keygen command, it create two file of public key and private key.
For our case it will create 4 file .(Here we use ed25519 algorithm to create SSH key.).For each keygen it ask you to make custom file name if any one want, so distinguish that's two file at working time, it is best choice to change file name with as you prefer.
[ By default this command generate file with name of id_{algorithm name}. so our case one file will be id_ed25519( private SSH key ) and other one is id_25519.pub ( as public SSH key ) ].
For our better development we need to change file name without changing id_{algorithm_name} part. Excluding change full name we add our prefer name to make unique this file we just add an extra name with this file. Like  id_ed25519_user_one.

> Finally we got four SSH key file :

`id_ed25519_user_one
id_ed25519_user_one.pub
id_ed25519_user_two
id_ed25519_user_two.pub`

Step 2: Now we need to make a config file to  say our git machine how it know which account we want now.
Make a config file without extension. Past below code

```
.ssh/config                                                  file
```

```
Host user_one github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_personal_user_one

Host user_two bitbucket.com
    HostName bitbucket.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_ed25519_personal_user_two
```

step 3: How to clone repo for different account ? 
Go to the github account . Press clone button it will pop up option on which option you want to clone. **Make sure you are going with SSH option .** I am make it clear later. 

Step 4: Open your terminal . Make clone command in your terminal.
In general case we clone repo like:

```
git clone git@github.com:user_name/DS-ALGO.git
```
But in this case we need to make a pretty change in our command. Since you make config file to make decision which account you want , that's why just change [ git@******:user_name/DS-ALGO.git ] *(astarick) part.
Now make command like:   

```
git clone git@user_one:user_name/DS-ALGO.git.
```

As a result now git can understand by which account you want to clone repo . 

And finally you are done with multiple git account.

Last question over this process is how we make push and all other command. Are those command work by this process ? 
Yes by this process all of the command and features of git will work pretty well. But you remember, I said in step 3 that make sure you clone your repo with SSH key. It is the main key of this way. When you clone your repo with SSH key it will automatically config your local directory( Local repo ) with proper config. To discover you can see .git folder in your local repo.

```
Location: .git/config

[core]
    .
    .
    .
[remote "origin" ]
    .
    . Find here
    .
    .
[branch "main"]
    .
    .
```