Isolating git and other configurations for work and personal projects can be complicated. In this post, I'll be sharing the way I isolate work development environment from personal one.
Setting up ssh
In order to isolate my work github account from my personal one when cloning, I've setup different host and identity for them. I utilize ed25519 as ssh key for both.
Below is a my ~/.ssh/config
which allows me to have two hosts github.com and gh-work both of which use different ssh keys.
Properties
Host github.comHostName github.comUser gitIdentityFile ~/.ssh/path-to-persona-keyHost gh-workHostName github.comUser gitIdentityFile ~/.ssh/path-to-work-key
So when cloning something for work, it becomes git@gh-work
instead of git@github.com
.
Separate Git configs
My personal git config lives in my dotfiles. For work related code, I've a dedicated repository named ~/Work
. In order to change git config inside this repository, I've put a gitconfig
in it's root which contains nothing more than my name and email for work.
Properties
[user]name = some-work-nameemail = my-work-email
and in order to apply this when I am under work directory, I've added following to my ~/.gitconfig
.
Properties
[includeIf "gitdir:~/Work/"]path = ~/Work/.gitconfig
This applies the work gitconfig whenever a git directory I am working with is inside work folder.
For other configurations
I have two shell scripts named home
and work
. When I land in a terminal, depending on where I want to work —
- Either a tmux session is created or I am attached to an already running tmux session.
- In both cases, these sessions contains session specific environment variables to interact with private registries and other things.
- I utilize gh, and I've two configs for it as well. Based on the session I'm being attached, The config symlink is switched to related config, to allow me to interact with that account on github.
This means, whenever I start a long session in terminal, I start in tmux with one of those two commands. This have worked so far.