Use separate Git for work and personal

If you use Git on one machine for work and personal projects, each commit should be tied to the right email address without constant fiddling.

I set up Git to pick the email based on which parent folder the project lives in. Set it up once and it works for every new project.

Organize projects by folder

This setup depends on keeping projects in separate top-level folders:

work/
├─ work-project-1/
├─ work-project-2/
personal/
├─ personal-project-1/
├─ personal-project-2/

Anything inside ~/personal/ signs commits with my personal email. Anything inside ~/work/ signs with my work email.

Set up: add a .gitconfig if statement

In ~/.gitconfig, add a conditional include for each folder:

[includeIf "gitdir:~/personal/"]
path = .gitconfig-personal
[includeIf "gitdir:~/work/"]
path = .gitconfig-work

Keep the trailing slash on the folder path. With it, Git matches every repo nested anywhere inside that folder.

The path is relative to the .gitconfig file itself, so these files sit right next to it in your home directory.

You can also set a global default for every other folder. Just put that first, with the if statements afterwards, like this.

# Default config
[include]
path = .gitconfig-default
# Directory-specific overrides
[includeIf "gitdir:~/personal/"]
path = .gitconfig-personal
[includeIf "gitdir:~/work/"]
path = .gitconfig-work

Remember to create the `.gitconfig-default` config as well.

Create a config file for each context

Next, create the two files and define a user in each.

~/.gitconfig-personal:

[user]
name = Your Name
email = you@personal.com

~/.gitconfig-work:

[user]
name = Your Name
email = you@work.com

You can put anything else that differs between contexts in these files too (a signing key, for example).

Verify the change

Go into any repo under one of those folders (personal or work) and check the email that’ll be used for commits:

Terminal window
git config user.email

Now every new project you create in either folder automatically commits with the right email.