I put all repos in /usr/local/src/wb and the following rule from .gitconfig picks repos up in that hierarchy and applies the .gitconfig-work file
~/.gitconfig:
[includeIf "gitdir:*/wb/"]
path = ~/.gitconfig-work
~/.gitconfig-work:
[user]
name = Andrew Cates
email = andrew@waybetter.com
[github]
user = wbandrew
password = redacted
token = redacted
This takes care of my user email config for git commits. I go a step further by tying keys to repos. In my ~/.ssh/config I have aliases like the following. I used to then run git clone git@wbandrew.github.com:wbetterdev/wb-graphql.git
to tie my repo to the id_andrew@waybetter.com
key. This worked good but added more variables to the mix than I had hoped for.
~/.ssh/config:
Host *
# Set ServerAliveInterval for all connections
ServerAliveInterval 120
ServerAliveCountMax 5
# http://interrobeng.com/2013/08/25/speed-up-git-5x-to-50x/
ControlMaster auto
ControlPath ~/.ssh/conn-%r@%h:%p
Host gist.github.com *.gist.github.com
Hostname gist.github.com
# See https://help.github.com/articles/using-ssh-over-the-https-port
Host ssh.github.com *.ssh.github.com 443.github.com *.443.github.com
Hostname ssh.github.com
Port 443
# This rule must stay below more specific host rules to avoid
# "Hostname" to be set to github.com
Host *.github.com
Hostname github.com
Host github.com gist.github.com ssh.github.com 443.github.com *.github.com *.gist.github.com *.ssh.github.com *.443.github.com
User git
# Force SSH2
Protocol 2
Host wbandrew.github.com
Hostname github.com
IdentitiesOnly yes
IdentityFile ~/.ssh/id_andrew@waybetter.com
Host wbandrew.gist.github.com
Hostname gist.github.com
IdentitiesOnly yes
IdentityFile ~/.ssh/id_andrew@waybetter.com
Host wbandrew.ssh.github.com wbandrew.443.github.com
Hostname ssh.github.com
IdentitiesOnly yes
IdentityFile ~/.ssh/id_andrew@waybetter.com
Port 443
However, I can tie the two techniques above to give best of both worlds. I add the following url.insteadOf configs and am able to git clone git@github.com:wbetterdev/wb-graphql.git
(no wbandrew) and still use my id_andrew@waybetter.com
ssh key. The insteadOf translates the urls behind the scene.
~/.gitconfig-work:
# this translates
# git@github.com:wbetterdev/wb-graphql.git (.git/config) -->
# git@wbandrew.github.com:wbetterdev/wb-graphql.git
[url "git@wbandrew.github.com"]
insteadOf = "git@github.com"
# this translates
# git@wbandrew.github.com:wbetterdev/wb-graphql.git (.git/config) -->
# git@github.com:wbetterdev/wb-graphql.git
[url "git@github.com"]
insteadOf = "git@wbandrew.github.com"
# this translates
# https://wbandrew.github.com/ -->
# https://github.com/
# [url "https://github.com/"]
# insteadOf = "https://wbandrew.github.com/"
# this translates
# https://wbandrew.github.com -->
# https://github.com
# [url "https://github.com"]
# insteadOf = "https://wbandrew.github.com"
This allows tools like hub.github.com
and others to just work out of the box. If you want to go a step further take a look at ondir to setup and teardown environments based on directory.