DEV Community

Kamil
Kamil

Posted on • Originally published at banach.net.pl on

Git config based on current (sub)folder

Some time ago I’ve looked over my current company repositories. Surprisingly there was a lot of commits with emails that are not company ones. Instead of those - a lot of gmail.com, _outlook.com_and other free email providers!

That looks a bit unprofessional, isn’t it? Even if we do some freelance gigs - we probably want to put nicer email than private one used for social websites. And if, for some reason, we’re forced to use company email - we need to be sure that it will be used!

So, what we can do to make it better? Just configure it with couple lines of code ;-) Let’s assume that all our work-related stuff will live in ~/work folder and all other is personal. So, let’s start with a simple ~/.gitconfig configuration:

# ~/.gitconfig
[user]
name = Johny Mnemonic
email = johny@mnemonic.tld
[includeIf "gitdir:~/work/"]
path = ~/work/.gitconfig

Enter fullscreen mode Exit fullscreen mode

Configuration in user section will be default one. We will overwrite it in other configuration files, like in folders with work.

Let’s then create an additional configuration for work, just in our work folder:

# ~/work/.gitconfig
[user]
email = johny@memory.courier.tld

Enter fullscreen mode Exit fullscreen mode

We want to put our name in commits, yep? So only one thing that we overwrite will be the email address. Instead of private one, there will be company email.

And that’s it. Now all repositories, that didn’t have set up individual user settings, will use ones that are based on the folder, e.g.:

  • ~/work/super-duper will use work configuration,
  • ~/project/my-website will use private (default) one.

I hope that “short writeup” will help somebody in some way. :-) Also - did you have any comments? Drop me a line on Twitter!

Top comments (0)