I use the same machines to work on both personal and work projects. I usually have to use a different
Git identity for the work projects than for my personal projects.
Previously I had my personal
Git identity set globally and then used local
Git configs to override it in work projects. This worked just fine but it was too much work. There is a better solution.
Git config allows you to use, or better to say include, another
Git config for a specific directory and all its subdirectories. I have all my projects stored in
~/Projects and subdirectories like
~/Projects/open-source and work projects in
I created a
~/.companyName.gitconfig that overrides just the name, email and GPG signing key to match the work identity
[user] name = Igor Kulman email = firstname.lastname@example.org signingkey = ABC
I then included this config in my main
~/.gitconfig just for the
[user] name = Igor Kulman email = email@example.com signingkey = DEF ... [includeIf "gitdir:~/Projects/CompanyName/"] path = ~/.companyName.gitconfig
to achieve exactly what I needed.
To verify and quickly check which
Git identity is being used in a specific
Git repository you can use this simple
[alias] whoami = "! git var -l | grep '^GIT_.*_IDENT'"