Git: Configuring Different Git User Info Depending on Projects

PROBLEM

Given, the following global config stored under ~/.gitconfig

[user]
    name = Shitty Developer
    email = shittydeveloper@work.com

When committing any code, the above user info will always be used.

However, there are times you want to use a different user info depending on projects (ex: work projects, GitHub projects, etc)

SOLUTION

This is one of many ways to solve this problem.

Let’s assume shittydeveloper@work.com configured in ~/.gitconfig is reserved for work-related projects.

To set up a different user email for GitHub projects, modify ~/.gitconfig with the following configuration:-

[user]
    name = Shitty Developer
    email = shittydeveloper@work.com

[includeIf "gitdir:~/github/"]
    path = ~/github/.gitconfig

Under ~/github/.gitconfig, add your GitHub user account’s email:-

[user]
    email = shittydeveloper@personal.com

If the checked out project resides under ~/github/ directory, any committed code will now use shittydeveloper@personal.com instead of shittydeveloper@work.com.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s