How to Work with Git and Multiple Accounts

In this article, I show how to use Git includeIf statements to include multiple configurations based on the directory you are currently in.

The Basic Setup of Git

Recently I had to set up a new machine. When setting up a new environment, one of the first things is also setting up Git.

This is rather straightforward, you install Git and configure some preferences to your need like setting your name and email. You can do so by running the following commands in your terminal.

git config --global
git config --global

This will save your settings in a global ~/.gitconfig file located in your home directory.

email = ""
name = "John Doe"

Of course, you could customize it even further, but let’s keep it simple for now.

Working with Different Accounts

The issue arises, when you are working with different projects, which might belong to different accounts like for example you are using your business email to work on your daily tasks but might also want to contribute to other open-source projects, using your private email.

Since Git allows you to create a local .gitconfig per project, you can technically set individual configs per project. Running git config without the --global flag will save app settings in the git config, relative to your project directory .git/config.

Although this approach does work for a single project, it does not scale when switching projects.

Git Includes

Luckily Git has a built-in feature to source different config files based on your current directory.

According to the official documentation it does the following:

The contents of the included file are inserted immediately as if they had been found at the location of the include directive. If the value of the variable is a relative path, the path is considered to be relative to the configuration file in which the include directive was found.

This is how it works in practice. In your global .gitconfig create two, or more, includeif statements, which will source different configuration files based on the matching directory. Shared configuration like the user's name or aliases could still go into the global config.

# ~/.gitconfig[includeIf "gitdir:~/Documents/private/GitHub/"]
path = ~/Documents/private/GitHub/.gitconfig
[includeIf "gitdir:~/Documents/work/GitHub/"]
path = ~/Documents/work/GitHub/.gitconfig
# shared configuration like user name, aliases or colors[user]
name = "John Doe"

Next, you have to create another two configs in the directories you just defined. In this example, I simply created a directory for all private and work-related projects.

└── Documents
├── private
│ └── GitHub
│ └── .gitconfig
└── work
└── GitHub
└── .gitconfig

Private Git config

# ~/Documents/private/GitHub/.gitconfig[user]
email = ""
signingkey = "ABC"

Work Git config

# ~/Documents/work/GitHub/.gitconfig[user]
email = ""
signingkey = "ZYX"

Of course, you could also add any other setting here if needed.

Further Resources



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marco Biedermann

Marco Biedermann


Full-Stack JavaScript Engineer with focus on React, Redux & Node.js and passioned about Open Source working @Mobime based in Berlin, Germany