Tweak your Git config for multiple user IDs

Use the same machine for your work and personal Git commits without manually resetting your config.
79 readers like this.
Chat via email

Git's git config command (hereafter referred to as "config") enables you to set repository or global options for Git. It has many options, and one of them, includeIf, is handy when you have dual roles using Git, for example, working full time as a developer and contributing to open source projects in your spare time. Most people in this situation don't want to use a common config for both roles or, at the very least, would rather keep certain portions of their config distinct, especially if they use the same computer in both roles.

This is my situation, so I keep two separate sets of mail IDs as part of my Git config. This way, commits in repositories related to projects at my workplace carry my office mail ID, whereas the commits made to repositories hosted in my personal GitHub account use my personal mail ID.

To illustrate, here is a sample snippet from my global config (maintained at $HOME/.gitconfig), which I will describe below.

[includeIf "gitdir:~/priv_scm/"]
        path = ~/priv_scm/.gitconfig
[includeIf "gitdir:~/work_scm/"]
        path = ~/work_scm/.gitconfig

What's includeIf?

The includeIf.condition.path variable, part of the include config directives, allows you to set a custom config conditionally. Also, the path to the custom config can be set as part of the directive.

This directive supports three keywords: gitdir, gitdir/I, and onbranch. I will briefly explain gitdir, which I use in the code snippet above; you can learn about the other two in the docs.

Using the gitdir keyword in the includeIf directive causes a conditional check to be performed on the pattern. Per the rule, if the current working directory matches the specified directory pattern in gitdir, then it picks the config from the given path. I'll apply this rule on the config snippet to show how it's evaluated.

In the config snippet, you can see a simple pattern, ~/, used along with the gitdir keyword. This pattern is substituted with the value stored in the $HOME environment variable.

How to use it

If you're using the same system to work on open source projects hosted on GitHub or GitLab and committing to Git repositories at work, you can have two top-level directories, like $HOME/priv_scm and $HOME/work_scm. Within these two directories, you can have two separate .gitconfig files with settings related to your user.name and user.email. However, they can also be included in a global .gitconfig stored at $HOME that can hold all the customizations common to both environments.

Here is a snippet of an example $HOME/priv_scm/.gitconfig:

$ cat $HOME/priv_scm/.gitconfig
[user]
    name  = Ramanathan Muthiah
    email = <personal-mailid-goes-here>

With this config in place, you can switch directories and start working on open source projects without resetting some of the config related to Git manually. These changes are handled automatically in the main .gitconfig with the aid of the includeIf conditional directive.

Git tips

I hope this tip helps you organize your Git projects. What are your favorite Git tips? Share them in the comments!

What to read next
Tags
Avatar
Ram works for a software firm in the South Indian city of Bengaluru, as a DevOps engineer. He is looking forward to spending time and effort to explore the real world on a bicycle. While not at work, he likes to read books.

Comments are closed.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.