Conditional Git Configuration
I’m about to start a new job that uses GitHub. I’ll be using my existing GitHub account but with a work email for any repositories in the work organization. To avoid acccidentally pushing commits with my personal email to a work repository, I’ve updated my git
configuration with a conditional include. These can use the path to the .git/
directory, the branch, or the remote to include another config file.
I’ll be using .example
domains throughout to avoid referencing any real emails or GitHub resources.
# ~/.config/git/config
[user]
name = Branton Boehm
email = branton@personal.example
[includeIf "hasconfig:remote.*.url:git@github.example:Work/**"]
path = config-work
# ~/.config/git/config-work
[user]
email = branton@work.example
I can verify this is working by checking the email from within a git
clone:
~/src/website $ git remote -v
origin git@github.example:branton/website.git (fetch)
origin git@github.example:branton/website.git (push)
~/src/website $ git config --get user.email
branton@personal.example
~/src/api $ git remote -v
origin git@github.example:Work/api.git (fetch)
origin git@github.example:Work/api.git (push)
~/src/api $ git config --get user.email
branton@work.example
If this isn’t working as expected, the git config --list --show-origin
command will display the config file each setting is coming from.