Brain Phrye

code cooking diy fiction personal photos politics reviews tools 


Git Config Includes

[ Listen to article ]

It turns out that you can include files in your ~/.gitconfig. One thing the docs don’t explain is that missing files are silently ignored. In addition you can include files conditionally based on where the git repo is and what the branch name is.

All these make things much easier for my home dir to work across environments.

For example I have a crypt repo which includes encrypted versions of my gpg keys. I only check it out on machines I keep secure (encrypted hard drives, not portable, etc) so I don’t always have gpg keys available. When I do have the keys though, I’d like to always sign commits.

The solution is to do this in my ~/.gitconfig:

1
2
[include]
	path = ~/.gitconfig.d/gpg

If the file exists it will set up autosigning, if not, nothing happens. There’s no error. Perfect!

I also write code for myself, for my employer and for some clients. I set my email in the [user] block, but I can override it for clients:

1
2
3
4
[includeIf "gitdir:~/src/employer/"]
	path = ~/.gitconfig.d/employer
[includeIf "gitdir:~/src/client1/"]
	path = ~/.gitconfig.d/client1

And then in ~/.gitconfig.d/client1 I have this:

1
2
[user]
	email = lyda@client1.com

I’m not quite sure how else this will be useful, but this alone should stop me committing with the wrong address which is a win in itself. Back when reviewboard was a thing it might be useful there too, but hopefully that’s in the past.