DEV Community

Cover image for Automatic dual git config
Cen
Cen

Posted on

Automatic dual git config

Introduction

Do you use the same computer for both personal and professional projects?
Do you have one git account for pro and one for personal?
Then follow this short guide and have everything set up for automatic git config switch.

What’s the idea

We will tell git through a configuration file to use the correct user info like name and email based on the path we are currently on when using git commands.

I use this setup on mac but it should work on Linux too.

I use VSCode as my IDE and its CLI to open and edit my files.

Realization

First step

Open your git config file

code ~/.gitconfig
Enter fullscreen mode Exit fullscreen mode

Second step

This is where the magic will works

[include]
    path = ~/pro-git.conf
[includeIf "gitdir:~/perso/"]
    path = ~/perso-git.conf

[pull]
    rebase = true
Enter fullscreen mode Exit fullscreen mode
  • first include is the generic configuration used.
  • second include with an if is a condition that tells git to use another specific conf when you are inside a specific directory.
  • A little extra I recommend is to set pull rebase to true inside your git config but that’s not related to what we are doing now. Check on the web for a clear explanation for this point!

As you can see I have 2 files at my root, 1 for each use case, pro and perso. Those files contain the related user info for each use case.

Third step

Fill in the information of your user inside both config files.

[user]
    name = Personal User
    email = personal@email.com
Enter fullscreen mode Exit fullscreen mode

and same for the pro file.

As you guessed this is reproducible for more than 2 users and config.

There are things to learn about writing a proper git config and what’s possible with it, this documentation gives a lot of information about it !

https://git-scm.com/docs/git-config#_configuration_file

Hope this helps you set up your computer for efficient work with git on all your projects!

Top comments (0)