Update (2018-03-28): if you have work and personal projects on the same machine, a better way to do this is to put all your work projects in one directory and use conditional configuration includes, introduced in Git 2.13.
I store my .gitconfig
in Git, naturally. It contains this block:
[user] email = will@willthompson.co.uk name = Will Thompson
which is fine until I want to use a different email address for all commits on my work machine, without needing git config user.email
in every working copy. In the past I’ve just made a local branch of the config, merging and cherry-picking as needed to keep in sync with the master
version, but I noticed that Git reads four different config files, in this order, with later entries overriding earlier entries:
/etc/gitconfig
– system-wide stuff, doesn’t help on multi-user machines$XDG_CONFIG_HOME/git/config
(aka~/.config/git/config
) – news to me!~/.gitconfig
$GIT_DIR/config
– per-repo, irrelevant here
So here’s the trick: put the standard config file at ~/.config/git/config
, and then override the email address in ~/.gitconfig
:
[user] email = wjt@endlessm.com
Ta-dah! Machine-specific Git config overrides. The spanner in the works is that git config --global
always updates ~/.gitconfig
if it exists, but it’s a start.
Alternatively, you can rely on the envirionment variables https://git-scm.com/book/en/v2/Git-Internals-Environment-Variables This way you could plug it into your standard shell dotfiles.
Ah, interesting, thanks for the pointer!
The pretty log alias you gave me years ago still serves me well!
This post isn’t really about Git, is it? 😛
Maybe not!
I have the below snippet at the end of ~/.gitconfig (shared between machines) to include ~/.gitconfig.local (not shared) and it seems to work well:
[include]
path = .gitconfig.local