« 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
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-personal3[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-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_user3 email = work@email
File → $HOME/.gitconfig-personal
1[user]2 name = personal_user3 email = personal@email
Just like that we are set !! Now you will be having three git files in your home directory.
Now we will create and initiate a new git repo in the work and personal directories and check the configurations.
1$ cd ~/work2$ mkdir work-test-repo3$ cd work-test-repo4$ git init5 *Initialized empty Git repository in /Users/dbarochiya/work/work-test-repo/.git/*6$ git config -l7 *credential.helper=osxkeychain8 includeif.gitdir:~/personal/.path=~/.gitconfig-personal9 includeif.gitdir:~/work/.path=~/.gitconfig-work10 **user.name=work_user11 user.email = work@email**12 core.repositoryformatversion=013 core.filemode=true14 core.bare=false15 core.logallrefupdates=true16 core.ignorecase=true17 core.precomposeunicode=true*
1$ cd ~/personal2$ mkdir personal-test-repo3$ git init4 *Initialized empty Git repository in /Users/dbarochiya/personal/.git/*5$ git config -l6 *credential.helper=osxkeychain7 includeif.gitdir:~/personal/.path=~/.gitconfig-personal8 **user.name=personal_user9 firstname.lastname@example.org**10 includeif.gitdir:~/work/.path=~/.gitconfig-work11 core.repositoryformatversion=012 core.filemode=true13 core.bare=false14 core.logallrefupdates=true15 core.ignorecase=true16 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