How to Work with Git and Multiple Accounts
Tags: github, tooling, webdev, tutorial
Published: 2021-04-28 13:03:39

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](https://git-scm.com/book/en/v2/Getting-Started-Installing-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.

```bash
git config --global user.name "John Doe"
git config --global user.email "john.doe@work.com"
```

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

```
[user]
  email = "john.doe@work.com"
  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](https://git-scm.com/docs/git-config#_includes) 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 = "john.doe@private.com"
  signingkey = "ABC"
```

### Work Git config

```
# ~/Documents/work/GitHub/.gitconfig

[user]
  email = "john.doe@work.com"
  signingkey = "ZYX"
```

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

## Further Resources

- [Official Git Documentation](https://git-scm.com/docs/git-config#_includes)
- [Stack Overflow Post](https://stackoverflow.com/questions/3860112/multiple-github-accounts-on-the-same-computer)

---

Thank you for reading, talk to you next time, and stay safe! 👋