DEV Community

Cover image for How to Work with Git and Multiple Accounts
Marco Biedermann
Marco Biedermann

Posted on • Edited on

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 user.name "John Doe"
git config --global user.email "john.doe@work.com"
Enter fullscreen mode Exit fullscreen mode

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

[user]
  email = "john.doe@work.com"
  name = "John Doe"
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Private Git config

# ~/Documents/private/GitHub/.gitconfig

[user]
  email = "john.doe@private.com"
  signingkey = "ABC"
Enter fullscreen mode Exit fullscreen mode

Work Git config

# ~/Documents/work/GitHub/.gitconfig

[user]
  email = "john.doe@work.com"
  signingkey = "ZYX"
Enter fullscreen mode Exit fullscreen mode

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

Further Resources


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

Top comments (0)