Learn to setup Git on your machine

Hello readers,

This post is targeted for folks who are planning to setup dual Git accounts on their machine.

In this post, I will show you steps on getting your machine linked with two different Git accounts at the same time.

Once done, you will be able to continue with coding work for your personal and work accounts simultaneously.

For reference, let's assume we have a personal Git account as john@github.com and a work Git account as john@xyz.us. Replace these example email addresses with real ones during implementation.

We will setup Git in a way that you can quickly toggle between these two email addresses at will and continue with your personal or professional work.

First step is to ensure you have Git already installed on your machine. Open up the terminal and run following command:

git -version

Once you have confirmed that Git is present, let us generate SSH keys for each Git account. To do that, run following command:

ssh-keygen -t rsa -C "john@github.com"

Above command will generate a SSH key for our personal account. Upon running the command, use absolute path and give it a unique name as following: /home/john/.ssh/id_rsa_personal. Use root as passphrase when asked.

Now, let's generate SSH key for our work Git account.

ssh-keygen -t rsa -C "john@xyz.us"

Follow the same steps like before. Only for file name and path, use following naming convention: /home/john/.ssh/id_rsa_work. Use same passphrase (root) like previously.

Once done, let's add our keys to SSH agents. Run following commands:

ssh-add ~/.ssh/id_rsa_personal

ssh-add ~/.ssh/id_rsa_work

After that, setup a SSH configuration file with custom content. First, run following command:

sudo nano ~/.ssh/config

Once a new file is created, add following content and save the file.

# Personal account
     Host 
github.com
     HostName 
github.com
     User git
     IdentityFile ~/.ssh/id_rsa_personal

# Work account
     Host xyz.us
     HostName xyz.us
     User git
     IdentityFile ~/.ssh/id_rsa_work

Final step would be to add SSH keys to your Personal and Work version control system accounts. To preview the content of SSH keys, use following commands:

cat ~/.ssh/id_rsa_personal.pub

cat ~/.ssh/id_rsa_work.pub

Copy the values and add them to remote systems.

One thing to note, if you have your personal account with GitHub, before cloning a project hosted on GitHub, generate a personal access token (classic) from your GitHub profile, copy the generated token value and clone any personal project locally (see command below). This way, your personal access token will be cached during first request and you won't have to reenter credentials again.

git clone https://john:personalaccesstoken@github.com/john/myproject.git

Congratulations, you have successfully setup dual Git accounts on your machine. To switch between them when needed, use following commands:

For personal account:

git config --global user.email "john@github.com"

For work account:

git config --global user.email "john@xyz.us"

Clone one remote project from each Git account. Make a minor code change and push it to remote. Validate the change on remote.

Happy coding!