introduction
My repository contains a bootstrap script that I use to setup the following folder structure:
home
├── .gitconfig
└── code
├── personal
│ └── .gitconfig
└── work
└── .gitconfig
I organize my projects into two directories: ~/code/personal for personal projects and ~/code/work for
work-related ones. Additionally, I've created symbolic links for three git configurations: one for each of the
aforementioned project directories, and a more general one in my home directory.
The overarching git configuration contains universal aliases and settings applicable to any project. These include
setting my editor to nvim, enforcing fast-forward merges, pruning during fetch operations, using rebase on
pull, etc.
The git configuration in ~/code/personal updates the email to my personal address. since this directory also serves
as the repository for my open-source contributions, it incorporates settings to sign all commits using my gpg key.
The final git configuration, designated for my ~/code/work directory, switches the email to my work address and
incorporates a commit template for conventional commits.
I also maintain two distinct ssh keys: one for personal/open-source projects and another for work. Given that most
companies require me to use their laptops, it's crucial for me to rotate these keys with each new client. It's
important to not retain old keys linked to your Github account, especially after returning the hardware to its owner.
Below, I'll provide instructions on how you can achieve a similar setup.
setup
We'll start with the global git configuration that we keep in our home directory:
[user]
editor = "nvim"
[alias]
unstage = reset
...
[includeif "gitdir:~/code/personal/"]
path = ~/code/personal/.gitconfig
[includeif "gitdir:~/code/work/"]
path = ~/code/work/.gitconfig
The key components in this configuration are the two [includeif] statements at the bottom. with these, we instruct git
to incorporate our specific configurations depending on the repositories path on our file system.
Now, this is what my personal config looks like:
[user]
email = victor@conner.dev
name = victor conner
signingkey = 15d...
[commit]
gpgsign = true
[gpg]
program = gpg
[url "git@github.com-personal"]
insteadof = git@github.com
Again, I've placed the interesting bit at the end. Here we're overriding the url of the repository to be
git@github.com-personal instead of git@github.com. This alteration is going to enable the automatic selection
between our two ssh keys.
If you don't have two separate ssh keys already you can go ahead and generate them like this:
ssh-keygen -t rsa -b 4096 -f ~/.ssh/work
ssh-keygen -t rsa -b 4096 -f ~/.ssh/personal
We can now add the following lines to your ssh config:
host github.com-work
hostname github.com
addkeystoagent yes
user git
identityfile ~/.ssh/work
identitiesonly yes
host github.com-personal
hostname github.com
addkeystoagent yes
user git
identityfile ~/.ssh/personal
identitiesonly yes
The right key will automatically get picked based on the project path. I've left out the git config I use for work,
and trimmed the others to highlight the relevant parts for this post. If you're curious about the full configuration you
check out my repository on Github.
The end
I usually tweet something when I've finished writing a new post. You can find me on Twitter
by clicking