DEV Community

Cover image for How to Work with Git and Multiple Accounts
World In Dev

How to Work with Git and Multiple Accounts

marcobiedermann profile image Marco Biedermann ・3 min read

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


πŸŽ‰ Giveaway

We are giving away any course you need on Udemy. Any price any course.
To enter you have to do the following:

  1. πŸ‘ React to this post
  2. βœ‰οΈ Subscribe to our newsletter

You will receive our articles directly to your inbox πŸ“¬.


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 "John Doe"
git config --global ""
Enter fullscreen mode Exit fullscreen mode

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

  email = ""
  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

  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

  email = ""
  signingkey = "ABC"
Enter fullscreen mode Exit fullscreen mode

Work Git config

# ~/Documents/work/GitHub/.gitconfig

  email = ""
  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! πŸ‘‹


Make sure to subscribe to our newsletter πŸ’Œ to receive our weekly recap directly on your email and react to this post to automatically participate in our giveaway 🎁

If you would like to join our discussions, you can find us on Discord πŸ“£.


Discussion (0)

Forem Open with the Forem app