Managing Multipple gitconfig Profiles on 1 Computer
Categories:
If you’re working on multiple repositories based on your work and personal projects. And you need to seperate gitconfig usernames and emails for each of scope, for example to show up your contributions on personal GitHub profile.
So, this post is for you!
Organize your repositories
First, you need to put whole repositories from each scope inside a distint directory. It help git
recognizes which type of project you’re working on when doing git
operations, then uses the corresponding gitconfig
profile.
For example, let’s say that you may want to seperate your work and personal workspaces:
~/git-repos/personal-projects/
→ For personal projects.~/git-repos/work-projects/
→ For work projects.
Modify global .gitconfig
Create the global .gitconfig
file in your home directory if it doesn’t exist. Then add the below to the file:
[includeIf "gitdir:~/git-repos/personal-projects/*/"]
path = ~/.gitconfig-personal
[includeIf "gitdir:~/git-repos/work-projects/*/"]
path = ~/.gitconfig-work
With this configuration, if the path where you created the git
directory matches one of the paths in inclideIF
, then the corresponding configuration file will be used.
Create individual .gitconfig
for each scope
If you haven’t noticed by now, we just mentioned the .gitconfig-personal
and .gitconfig-work
files in the global .gitconfig
file, but we didn’t create them yet. These individual files can contain all the customization that you need, from user name and email to commit hooks.
Add this to .gitconfig-work
file from your home directory:
[user]
name = work_username
email = [email protected]
And add this to .gitconfig-personal
file
[user]
name = personal_username
email = [email protected]
Let’s check
We’re all set! Now we will create and initiate a new git
repository in the personal workspace and check the configurations.
cd ~/git-repos/personal-projects/
mkdir personal-test-repo
cd personal-test-repo
git init
Then you can validate your work with git config -l
, the result should be like this:
$ git init
*Initialized empty Git repository in ~/git-repos/personal-projects/personal-test-repo/.git/*
$ git config -l
...
user.name=personal_username
[email protected]
...