Can gitconfig options be set conditionally?

No, Git config does not support checks or conditional statements. But your underlying shell probably does, so you can use something like:

[core]
    editor = "if [[ $IS_REMOTE -eq 1 ]]; then ED='vim'; else ED='subl -n -w'; fi; $ED"


If you need to do something more complicated than that, you could just throw the shell code into a script, of course, like

[core]
    editor = "my_edi_script.sh"

with my_edit_script.sh containing something like:

#!/bin/bash
if [[ $IS_REMOTE -eq 1 ]]; then
    ED="vim"
else
    ED="subl -n -w"
fi

$ED some argument or other

Edit: The my_edit_script.sh would have to be in the $PATH, of course :)


The [include] section learned by git-config in v1.7.9 gets you most of the way there.

While it doesn't let you write runtime conditionals, it does give you a framework for refactoring your ~/.gitconfig into several parts: the shared section, and the env-specific sections. After that, you can symlink something like ~/.gitconfig.local to the relevant env-specific config file, and include ~/.gitconfig.local from ~/.gitconfig.

The symlinking part can be scripted and done automatically as part of your dotfiles' init script.

From the command line, that include path can be added via:

git config --global include.path '~/.gitconfig.local'

I use the quotes above specifically to prevent the shell from expanding ~ to an absolute path.

That adds the following section to your ~/.gitconfig:

[include]
    path = ~/.gitconfig.local

Here's a snippet from the git-scm book showing the general format:

[include]
    path = /path/to/foo.inc ; include by absolute path
    path = foo ; expand "foo" relative to the current file
    path = ~/foo ; expand "foo" in your $HOME directory