This may be overkill, but it works on my machine
How I configure my Git identities
Note: I've had this post drafted for 3 YEARS!!! It's finally time to publish it.
I like to mess with my dotfiles and every so often, I find out about a new way to do things and I spend more time than I should learning how to use it.
A few years ago I learned about includeIf for including specific files if some condition was met for git
. The example that I first saw was doing:
So that ~/.config/git/personal
is only included for git
directories under ~/code
and ~/.config/git/work
is only included for directories under ~/work
. The contents of those included files varies but usually it contains your git identity, signing keys, etc. Here's an example of what that could look like:
That works pretty well but I usually organize all my code in ~/workspace
regardless of whether its personal, work-1, work-2, etc. I wanted to be able to configure git depending on where that repo actually lives instead of where the directory is in my machine. Then I found out about hasconfig:remote.*.url:!
This makes it so that I can configure git conditionally if the given remote URL exists for that directory I'm currently working in.
A few examples of what I do is:
💡 EDIT: The order of these matter as git will include the last matching config. In this case,
github.com:orgname/**
has to go below the generalgithub:*/**
otherwise the default github config will overwrite the one fororgname
. Thanks catching this typo Fede.
Now if I'm in a directory where the remote matches github.com:orgname/**
it would use ~/.config/git/config-gh-org
, otherwise it uses the general config file for any other GitHub repo.
While that handles git identities, I still need to configure SSH keys separately to be able to pull
and push
to remotes. The simple version of my ~/.ssh/config
looks like this:
💡 EDIT: Depending on how your
ssh-agent
is configured, it may be a good idea to addIdentitiesOnly yes
after theIdentityFile
line for eachHost
in you~/.ssh/config
. Thanks for the heads up Jorge.
The only problem with this is that in order to use a different IdentityFile
for the same Hostname
so that I could use a different key for repos under github.com/orgname
, I'd have to use a different value for Host
. So in my case I would add the following to my ~/.ssh/config
:
Finally, to use that Host
when I'm looking for a repo in github.com/orgname
, I would add the following to my git config:
So when I clone
, pull
, or push
a repo that's under my work's org account I can do:
and insteadOf
would replace github.com:orgname
with gh-work:orgname
so that it uses the right info from my SSH config. It's a neat trick which I saw referenced in this article.
Are there any issues with this approach? Is there a better way to do this? I'm not sure so please let me know as I'd love to learn and I'll update this post accordingly.