Separate git workspace configs with direnv
This article outlines a process for loading a custom
git configuration when in a specific directory tree.
This sets up a sort of local git configuration
per-directory without needing to alter the global
.gitconfig file or any other global
git or ssh configurations.
See here an example .envrc file. This file would be
used by direnv to set per-directory
env variables with the
direnv shell helper.
PATH=$PATH:$(pwd)/bin
export GIT_AUTHOR_EMAIL="Email to use for this organization"
export GIT_AUTHOR_NAME="Name to use for this organization"
export GIT_COMMITTER_EMAIL="Email to use for this organization"
export GIT_COMMITTER_NAME="Name to use for this organization"
export GIT_SSH="my-special-ssh-command-for-this-organization.sh"
Setting GIT_SSH allows for customizing the
ssh command and identity used for this directory.
See here for a sample custom ssh wrapper script. This uses a
specific ssh config file without needing a global
alias configuration. This file would be in ./bin/,
which is now in PATH thanks to the
.envrc config.
#!/usr/bin/env bash
ssh \
-F ~/whatever/directory/with/my/ssh/config \
-o IdentitiesOnly=yes \
-o GlobalKnownHostsFile=/dev/null \
-o UserKnownHostsFile=/dev/null \
"$@"
That ssh script wrapper is used in conjunction with a one-off
config file specifying where the keys are located.
Host *
AddKeysToAgent no
IdentitiesOnly = yes
IdentityFile ~/whatever/directory/with/my/ssh/id_rsa
StrictHostKeyChecking=no
Now any git commit and push in this directory tree
will leverage the env vars and scripts specified in
.envrc.