I recently had the need to use multiple identities when writing commits in Git
from a single laptop. I have a standard
.gitconfig file which contains my
preferred settings for colours, aliases, etc. This file also holds my personal
email address that I use for GitHub.
# ~/.gitconfig [user] email = firstname.lastname@example.org name = Chris Forbes
As I wanted to use my work email for work related projects, I did some quick research to find that Git has offered conditional inclusion of settings since version 2.13.
With this in mind, I can update my
.gitconfig file to have my default settings,
but also include a reference to a separate file to
be used when in a sub-directory of a particular path.
# ~/.gitconfig [user] email = email@example.com name = Chris Forbes [includeIf "gitdir:~/dev/work/"] path = ~/.gitconfig.work
On Windows it’s slightly different and we use
gitdir/i (case insensitive)
to also make it work with Visual Studio Code.
# ~/.gitconfig [user] email = firstname.lastname@example.org name = Chris Forbes [includeIf "gitdir/i:C:/dev/work/"] path = ~/.gitconfig.work
The included file contains any settings that need to be overridden.
# ~/.gitconfig.work [user] email = email@example.com
This means that when I am in a directory like
and I make a commit, it will use the work email address instead of the personal
email address. A nice little addition that makes working with different
identities much simpler.