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)

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.