TIL: git supports conditional config

for a situation that requires multiple github accounts. you need to be able to toggle a number of config elements to make life happy.

git has support for conditional inclusion in the configuration files. if you structure your config and directory structures appropriately you can use the gitdir status to selectively include git configuration elements into the mix, allowing you to toggle between one profile and another.

example

in the top level config (~/.gitconfig) i have the following

[include]
  path = ~/.gitconfig-conditional

~/.gitconfig-conditional has the directory specific inclusion logic.

[includeIf "gitdir:~/src/foo/"]
  path = ~/.gitconfig-foo

[includeIf "gitdir:~/src/personal/"]
  path = ~/.gitconfig-personal

the contents of the gitconfig-personal file has the individual config elements that need to be toggled to interact with the repos accordingly.

[user]
  name = steve ulrich
  email = sulrich@botwerks.org

[github]
  user = sulrich

[credential]
  username = sulrich

a quick comment re: credential.username this is particularly important and prevents you from having to tickle the git url in the individual repos to use the correct github username. by default git doesn’t seem to have an awareness of the username to associated with various profiles when using http repo urls.

this will result in multiple cached credentials within the git credential manager.