Git -- How to manage multiple ssh keys; includeIf might help!

An issue I kept running into was wondering how I can keep multiple ssh keys while using git. An ssh key for business related Github account, an ssh key for my person, and an ssh key for a given client. Here's a quick tip on how to set it all up!

First, you can store your root related information within your ~/.gitconfig file.

[user]
	name = Michael Nunez
	email = mnunez903@gmail.com

[alias]
	co = checkout
	br = branch
	ci = commit
	st = status
[push]
	autoSetupRemote = true
[core]
	editor = nvim

You can fill up your .gitconfig file with good basic root information that you would want for your global git config.

Then, you can add these things:

[includeIf "gitdir:~/src/business/clients/bobby-client/"]
	path = ~/src/business/clients/bobby-client/.gitconfig-bobby-client
[includeIf "gitdir:~/src/personal/"]
	path = ~/src/personal/.gitconfig-personal

A few things to note.

  • I keep programming related things under ~/src. Not sure where this came from.
  • I like to ensure things start from the top, which is why I often use ~/directories Not sure if there's a way to relative path it.
  • Those aliases are my go to.
  • Neovim is pretty chill, although I'm no vim expert. I just think anything git related has to be done in vim fashion, and all the cool kids are using Neovim.

Now, in .gitconfig-bobby-client I can have some specific configurations for that directory:

[user]
	email = michael.nunez@giobytes.io

Just a simple email change. I like that.

Given that I am using a business computer, I have the business related ssh key set to the defaults. Now, say, for personal stuff, I can now add the following:

[user]
	name = Michael Nunez
	email = mnunez903+personal@gmail.com
[core]
	sshCommand = "ssh -i ~/.ssh/id_ed25519_personal -F /dev/null"

Now, the ssh key that's associated to my personal work would be loaded whenever I run any git commands within the personal directory.

SWEET!