How to configure your gitconfig to use both personal and work git accounts on same dev machine.

Deepak Raj
3 min readAug 15, 2020

Gitconfig is very configurable tool and also helps us define custom alias commands or user configuration parameters.

Pre-requisite (setup):

Once git config has been setup to be used with work account using the first time setup guide, verify that the git config is accessible from bash/terminal and displays the configurations defined in git config using the following command.

$ git config --list

The contents of configuration file can also be viewed using Visual Studio Code by running the following command:

code $HOME/.gitconfig

How to configure .gitconfig to support multiple accounts

Create configuration settings for user (default), personal and work inside .gitconfig file as shown below:

The contents of gitconfig file showing user, personal and work account settings.

Create simple alias inside .gitconfig file to know the current active git account being used inside any work folders:

[alias] 
whoami= !git config --global user.name && git config --global user.email

Result (displays values from user config values in gitconfig):

To switch git to use personal github account lets create a new alias shortcut for git inside gitconfig which will help us easily switch from work account to personal account named “setpersonal”. Just add the new alias as shown below:

[alias]
whoami= !git config --global user.name && git config --global user.email
setpersonal= !git config --global user.name \""$(git config personal.name)"\" && git config --global user.email "$(git config personal.email)" && git whoami

Result:

Now, to switch back to use work git account globally lets add another alias into the git config file shown below as “setwork”:

[alias]
whoami= !git config --global user.name && git config --global user.email
setpersonal= !git config --global user.name \""$(git config personal.name)"\" && git config --global user.email "$(git config personal.email)" && git whoami setwork= !git config --global user.name \""$(git config work.name)"\" && git config --global user.email "$(git config work.email)" && git whoami

Result:

Sample .gitconfig configuration:

[user]
email = NAME@xyz.com
name = NAME
[personal]
email = NAME@gmail.com
name = NAME
[work]
email =NAME@xyz.com
name = NAME
[alias]
whoami= !git config --global user.name && git config --global user.email
setwork= !git config --global user.name \""$(git config work.name)"\" && git config --global user.email "$(git config work.email)" && git whoami
setpersonal= !git config --global user.name \""$(git config personal.name)"\" && git config --global user.email "$(git config personal.email)" && git whoami

The config settings for work or personal can be extended to use paths for git public key files for respective accounts.

Additional Tip: To reload gitconfig without restarting terminal window we can create another shortcut but this time inside “.bashrc” file as shown below:

alias restart=’source ~/.bashrc’

This will help us to just type command $restart and this will restart the terminal session reloading all new gitconfig settings or other bash settings changed after the terminal session has been started.

Useful Links & References:

  1. Git Config Setup Guide
  2. Git Alias

--

--