« How to handle multiple git configurations in one machine

You might get hard time mangaing many cats but when it comes to git profiles there is something we can do

cover.jpg

Let's get straight to the solution - The answer lies in the .gitconfig file - this is starter point for the git to identify what configurations needs to be used. The idea is to segregating the repos on your machine into multiple directories by separating the profiles you want with and then define .gitconfig file per profile.


Step 1 → create separate directories for repos

This is where you start - organise the projects that you are working on into separate folders by the profiles you want to work with. For example let's say there are two git profiles you are working, this use case would represent most of us.

  • WORK → for work related projects
  • PERSONAL → for open source and side projects

Step 2 → create global git configuration

Create the global .gitconfig file into your home directory if it doesn't already exists and add all the profile directories as an entry like the below file. It is very much intuitive what it does - if your directory path where you create the git directory matches with one of the path in inclideIF then use that particular profile specific configuration file otherwise use the default configurations.

File → $HOME/.gitconfig

1[includeIf "gitdir:~/personal/"]
2 path = ~/.gitconfig-personal
3[includeIf "gitdir:~/work/"]
4 path = ~/.gitconfig-work

Step 3 → create individual git configurations for profiles

If you haven't noticed by now - we have just mentioned the .gitconfig-personal and .gitconfig-work files in the global gitconfig files but we are yet to create them. These individual files can contain all the customisation that you need to perform form user name, and email to commit hooks.

FIle → $HOME/.gitconfig-work

1[user]
2 name = work_user
3 email = work@email

File → $HOME/.gitconfig-personal

1[user]
2 name = personal_user
3 email = personal@email

Let's verify

Just like that we are set !! Now you will be having three git files in your home directory.

asset-1.png

Now we will create and initiate a new git repo in the work and personal directories and check the configurations.

1$ cd ~/work
2$ mkdir work-test-repo
3$ cd work-test-repo
4$ git init
5 *Initialized empty Git repository in /Users/dbarochiya/work/work-test-repo/.git/*
6$ git config -l
7 *credential.helper=osxkeychain
8 includeif.gitdir:~/personal/.path=~/.gitconfig-personal
9 includeif.gitdir:~/work/.path=~/.gitconfig-work
10 **user.name=work_user
11 user.email = work@email**
12 core.repositoryformatversion=0
13 core.filemode=true
14 core.bare=false
15 core.logallrefupdates=true
16 core.ignorecase=true
17 core.precomposeunicode=true*
1$ cd ~/personal
2$ mkdir personal-test-repo
3$ git init
4 *Initialized empty Git repository in /Users/dbarochiya/personal/.git/*
5$ git config -l
6 *credential.helper=osxkeychain
7 includeif.gitdir:~/personal/.path=~/.gitconfig-personal
8 **user.name=personal_user
9 user.email=personal@email.com**
10 includeif.gitdir:~/work/.path=~/.gitconfig-work
11 core.repositoryformatversion=0
12 core.filemode=true
13 core.bare=false
14 core.logallrefupdates=true
15 core.ignorecase=true
16 core.precomposeunicode=true*

Voila - see the different email and user name is changed in both case. Depending on the path of the git repo it is able to use the custom gitconfig files.