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 = firstname.lastname@example.org 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
~/.config/git/config) – news to me!
$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
[user] email = email@example.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.