28 March 2020

Git for work and for you

Do you have your work email provided by your employer? If so it’s probably a good thing to use it in your git configuration while you are committing something for your company.

What about commit templates? hooks? It’s also a good thing to have configured the way a company requires!

But what about you? Your own commitment to open source software or anything else? I would like to have my personal email for that matter! Bet you too =] (Especially if it’s done in your own/free time)

There is a variety of solutions to have both on one machine! DirEnv is an awesome project but we can use git native solutions!

The lowest entry point is to do it on per project basis. A project config is stored in .git/config of a given project and is modified with the git config command when you do not pass --global or --system flags. But it’s a hell to maintain once you have more than a couple of personal and work related projects.

Here comes the hero, includeIf!

A bit of context. There is an include section in git config file that allows you to move some mess out.

Lets say you have an awesome [alias] section but it’s huge which makes your ~/.gitconfig harder to read and manage.

Example of a neat alias:

[alias]
	lg = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''	%C(white)%s%C(reset) %C(dim green)- %an%C(reset)' --all

include allows you to put it into separate file (let it be ~/.config/git/alias here) and yeah include it in the main one like this

[include]
	path = ~/.config/git/alias

Since it is not specific to aliases you can put almost any part of config in there!

includeIf takes it much further than just debloating. Now there is a condition! And we going to use one (path by gitdit). Config you put there overrides the main one.

My preference is to have my personal info in ~/.gitconfig and work related stuff in ~/.config/git/companyname.conf included by:

[includeIf "gitdir:~/work/"]
	path = ~/.config/git/companyname

In ~/.config/git/companyname I override [user] section and add [commit] section with commit templates.

This way work config is applied whenever project is under ~/work/ and regular one works everywhere else.

Sure it’s a simple example and you are welcome to go deeper and cooler!

For more info use man git-config

Good luck tinkering with stuff!